Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following Codes If a user click the checkbox and click btnRemove button in the gridview named grdSpareParts. then the row that is checked will remove.
My question is that when we put a row from a gridview to datatable Is it removed from that Gridview? And what is the function of DataSourceID? Please help me.
C#
protected void btnRemove_Click(object sender, EventArgs e)
{
    try
    {
        DataTable DTLocal = new DataTable();
        DTLocal.Columns.Add("SPCode");
        DTLocal.Columns.Add("MDate");
        DTLocal.Columns.Add("PDes");
        DTLocal.Columns.Add("SpareParts");

        for (int K = 0; K < grdSpareParts.Rows.Count; K++)
        {
            DataRow DRLocal = DTLocal.NewRow();
            DRLocal["SPCode"] = grdSpareParts.Rows[K].Cells[1].Text;
            DRLocal["MDate"] = Convert.ToDateTime(grdSpareParts.Rows[K].Cells[2].Text.ToString());
            DRLocal["PDes"] = grdSpareParts.Rows[K].Cells[3].Text.ToString();
            DRLocal["SpareParts"] = grdSpareParts.Rows[K].Cells[4].Text;
            CheckBox cb = (CheckBox)grdSpareParts.Rows[K].Cells[0].FindControl("chkCheck");
            if (!cb.Checked)
            {
                DTLocal.Rows.Add(DRLocal);
            }

        }
        grdSpareParts.DataSourceID = null;
        grdSpareParts.DataSource = DTLocal;
        grdSpareParts.DataBind();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
    }
}
Posted
v3
Comments
David Melcher 18-May-13 2:12am    
If I unterstand you correctly you want to remove the selected row if the user click the remove button, right?
Sumon562 18-May-13 2:16am    
Thanks for realizing

1 solution

Quote:
My question is that when we put a row from a gridview to datatable Is it removed from that Gridview?

GridView is just a view of the Data, which you bind to it using any DataSource like DataSet or DataTable.

If, you want to remove any row from GridView, then you need to delete that row from the DataSource that is the DataSet or DataTable or directly from database table.
And after every operation, you need to bind the GridView again using the same DataSource, so that it will contain the updated records, which will exclude that removed ones.

Refer - ASP.NET - What is the difference of DataSourceID and DataSource?[^].
Quote:
DataSource refers to actual data source object which can be .NET provided data source controls (such as ObjectDataSource, SqlDataSource) or actual data objects such as DataTable, Collection of objects etc.

DataSourceID is the string identifier for .NET provided data source control and this property exists so that data-bound control and corresponding data source can be associated at the design time in markup. Internally, the control would look up for actual data source control using the id provided.
 
Share this answer
 
v2

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