.Net Gridview数据绑定问题 [英] .Net Gridview databind problem

查看:64
本文介绍了.Net Gridview数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中如果用户单击复选框并单击名为 grdSpareParts 的gridview中的 btnRemove 按钮。然后检查的行将被删除。

我的问题是,当我们从gridview中添加一行到datatable时是否从Gridview中删除了它? DataSourceID 的功能是什么?请帮我。

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.

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
    {
    }
}

推荐答案

Quote:

我的问题是当我们从gridview中添加一行到datatable时它从Gridview中删除了吗?

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



GridView 只是数据的一个视图,你使用任何<$ c绑定它$ c> DataSource 喜欢 DataSet DataTable



如果,你想从 GridView 中删除​​任何行,那么你需要从 DataSource中删除该行 DataSet DataTable 或直接来自数据库表。

在每次操作之后,您需要使用相同的 DataSource 再次绑定 GridView ,以便它包含更新的记录,将排除已删除的记录。



参考 - ASP.NET - DataSourceID和DataSource的区别是什么? [ ^ ]。


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 指实际数据源对象,它可以是.NET提供的数据源控件(如ObjectDataSource,SqlDataSource)或实际数据对象,如DataTable,对象集合等。



DataSourceID 是.NET提供的数据源控件的字符串标识符,并且此属性存在,以便数据绑定控件和相应的数据源可以在设计时关联标记。在内部,控件将使用提供的id查找实际的数据源控件。

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.


这篇关于.Net Gridview数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆