从数据表中删除数据行

发布于 2024-11-03 17:47:53 字数 385 浏览 1 评论 0原文

假设我有一个数据表 dt(它包含广告商),并且我想从 dt 中删除一行,其中advertiserID 等于一个值,我该怎么做?

DataTable dt = new DataTable();
//populate the table
dt = DynamicCache.GetAdvertisers();

//I can select a datarow like this:
DataRow[] advRow = dt.Select("advertiserID = " + AdvID);

//how do you remove it, this get's me an error
dt.Rows.Remove(advRow)

那么如何正确地做到这一点呢?

谢谢。

Lets say I have a datatable dt (it contains advertisers) and I want to remove a row from dt where the advertiserID equals a value, how do I do that?

DataTable dt = new DataTable();
//populate the table
dt = DynamicCache.GetAdvertisers();

//I can select a datarow like this:
DataRow[] advRow = dt.Select("advertiserID = " + AdvID);

//how do you remove it, this get's me an error
dt.Rows.Remove(advRow)

so how do you do it correctly?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-11-10 17:47:53

advRow 是一个数组。您必须确定要删除数组中的哪一行。

dt.Rows.Remove(advRow[0]);

当然,这只是将其从数据表中删除,不一定是其背后的数据源(sql,xml,...)。这将需要更多...

并且在选择后检查数组或迭代数组将是一个好主意...

var datatable = new DataTable();
            DataRow[] advRow = datatable.Select("id=1");
            datatable.Rows.Remove(advRow[0]);
            //of course if there is nothing in your array this will get you an error..

            foreach (DataRow dr in advRow)
            {
                // this is not a good way either, removing an
                //item while iterating through the collection 
                //can cause problems.
            }

            //the best way is:
            for (int i = advRow.Length - 1; i >= 0; i--)
            {
                datatable.Rows.Remove(advRow[i]);
            }
            //then with a dataset you need to accept changes
            //(depending on your update strategy..)
            datatable.AcceptChanges();

advRow is an ARRAY. You have to identify which row in the array to delete.

dt.Rows.Remove(advRow[0]);

of course, this only removes it from the datatable, not necessarily the data source behind it (sql, xml, ...). That will require more...

and it would be a good idea to check the array or iterate the array after the select...

var datatable = new DataTable();
            DataRow[] advRow = datatable.Select("id=1");
            datatable.Rows.Remove(advRow[0]);
            //of course if there is nothing in your array this will get you an error..

            foreach (DataRow dr in advRow)
            {
                // this is not a good way either, removing an
                //item while iterating through the collection 
                //can cause problems.
            }

            //the best way is:
            for (int i = advRow.Length - 1; i >= 0; i--)
            {
                datatable.Rows.Remove(advRow[i]);
            }
            //then with a dataset you need to accept changes
            //(depending on your update strategy..)
            datatable.AcceptChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文