删除datagridview中的已检查项目? [英] Delete checked item in datagridview ?

查看:66
本文介绍了删除datagridview中的已检查项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除已在datagridview中检查过的项目。但是文件也印刷了两倍。



我尝试了什么:



I want to delete item that has been checked in datagridview. but document also print double.

What I have tried:

for(int xyz=0;xyz<datagridview1.rows.count;xyz++)>
            { 
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[xyz].Cells["Calckeck"];
                if (Convert.ToBoolean(chk.Value) == true)
                { 
                    DataTable dt = blord.getallfromorder_id(Convert.ToInt32(dataGridView1.Rows[xyz].Cells["cal_order_id"].Value));
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataGridViewRow dr in dataGridView1.Rows)
                        {
                            decimal total_to_be_remove = Convert.ToDecimal(dr.Cells["cal_total"].Value);
                            txtgrandtotal.Text = (Convert.ToDecimal(txtgrandtotal.Text) - Convert.ToDecimal(dataGridView1.Rows[xyz].Cells["cal_total"].Value)).ToString();
                            txtsubtotal.Text = (Convert.ToDecimal(txtgrandtotal.Text) - Convert.ToDecimal(dataGridView1.Rows[xyz].Cells["cal_total"].Value)).ToString();
                            lblsub_total.Text = txtsubtotal.Text;
                            printcalcel();
                            int i = blord.deleteitemfromorder(Convert.ToInt32(dataGridView1.Rows[xyz].Cells["cal_order_id"].Value));
                            dataGridView1.Rows.RemoveAt(xyz);
                        }                       
                    }

推荐答案

使用 .RemoveAt() foreach 循环中遇到麻烦,还有其他方法。

DataTable可以过滤数据,你可以使用 DataTable.Select()来过滤你的数据表,这是一个例子:

DataTable.Select Method(String)(System.Data) [ ^ ]

Using .RemoveAt() inside a foreach loop is asking for trouble, there are other ways.
DataTable can filter the data, you could use DataTable.Select() to filter your data table, here is an example:
DataTable.Select Method (String) (System.Data)[^]
var expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
    Console.WriteLine(foundRows[i][0]);
}


这篇关于删除datagridview中的已检查项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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