Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BiggestBookWeb.BLHelper;

namespace BiggestBookWeb.Forms
{
    public partial class MyList : System.Web.UI.Page
    {
        DataSet oDS = new DataSet();
        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             
                int counta = Convert.ToInt32(Session["AddToListCount"]);
                for (int i = 0; i <= counta; i++)
                {
                    string keyword = Convert.ToString(Session["AddToList" + i]);

                    oDS = SearchHelper.getCompareResultByKeywords(keyword);
                    if (i == 0)
                    {
                        foreach (DataColumn col in oDS.Tables[0].Columns)
                        {
                            dt.Columns.Add(col.ColumnName, col.DataType);
                        }
                    }
                    if (keyword != "")
                    {
                        dt = tblGridRow();
                    }
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
        }




        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {




            if (e.CommandName == "Remove")
            {
                int count = Convert.ToInt32(Session["AddToListCount"]);
                for (int i = 0; i <= count; i++)
                {
                    Session["AddToList"] = "";
                    Response.Redirect("../Forms/MyList.aspx");
                }
            }
        }
        
        public DataTable tblGridRow()
        {
            
            for (int i = 0; i < 1; i++)
            {
                DataRow dr = dt.NewRow();
                dr["LG_ITEM_DSC"] = oDS.Tables[0].Rows[0]["LG_ITEM_DSC"].ToString();
                dr["MFR_SKU_NUM"] = oDS.Tables[0].Rows[0]["MFR_SKU_NUM"].ToString();
                string Price = oDS.Tables[0].Rows[0]["LIST_AMT"].ToString();
                int length = Price.Length;
                dr["LIST_AMT"] = Price.Remove(length - 2, 2);
                dr["BRND_LOGO"] = oDS.Tables[0].Rows[0]["BRND_LOGO"].ToString();
                dr["INV_UN_CDE"] = oDS.Tables[0].Rows[0]["INV_UN_CDE"].ToString();
                dt.Rows.Add(dr);
            }
            return dt;
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView1.DataSource = dt;

            GridView1.DataBind();

            

        }
     }
}
Posted
Updated 7-Aug-13 19:59pm
v2
Comments
[no name] 8-Aug-13 2:07am    
Can you tell me why are you redirecting the page using "Response.Redirect("../Forms/MyList.aspx")"? Is it the same page or another page?

1 solution

HI,

If the page is MyList.aspx page then you need not to redirect the page again and again. Just make sure the grid binding in a separate function and call the function/bind the grid in place of
Response.Redirect("../Forms/MyList.aspx");

This will resolve your problem.

[Edit]

If you want to delete a row from the grid then use the following code.

C#
if (e.CommandName == "Delete")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridView1.DeleteRow(index);
        ((DataTable)ViewState["DataTable"]).Rows[index].Delete();
        ((DataTable)ViewState["DataTable"]).AcceptChanges();
        GridView1.DataSource = (DataTable)ViewState["Data"];
        GridView1.DataBind();
    }


In this way you can delete a single row.

[/Edit]


Thanks
 
Share this answer
 
v2
Comments
Member 10184990 8-Aug-13 2:44am    
i do it but all products are deleted....what can i do.. plz suggest me
[no name] 8-Aug-13 3:01am    
Is your requirement for deleting the row on remove button click?
[no name] 8-Aug-13 3:06am    
Check my updated solution for deleting the particular row.
Member 10184990 8-Aug-13 3:15am    
but error comes out on this line:GridView1.DeleteRow(index);

and error is The GridView 'GridView1' fired event RowDeleting which wasn't handled.
[no name] 8-Aug-13 3:40am    
Check out this link for the reference and use the rowdeleting event instead of the rowcommand.
Follow : http://stackoverflow.com/questions/15403231/gridview-delete-row

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900