C#删除所有Checked DataGridViewCheckBoxColumn行 [英] C# Delete all Checked DataGridViewCheckBoxColumn rows

查看:170
本文介绍了C#删除所有Checked DataGridViewCheckBoxColumn行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有DataGridView,其中包含4个colums,第一个叫做"Status",它是DataGridViewCheckBoxColumn,因为它是以设计师形式设计的 - Visual Studio 2008 express,通过拖动DataGridView然后编辑列和列类型。

现在,我成功检查所有行,执行以下操作:

public void SelectAllCheckBoxes()
{
for(int i = 0; i< EmployeeDataGridView.Rows.Count; i ++)
{
EmployeeDataGridView [i,0] .Value = true;
}

现在,如何在按下Delete按钮后从DataGridView中删除所有已检查的行?
希望有人能告诉我。
我想应该是一个快速的方式..

谢谢你

Hi,

I have DataGridView which includes 4 colums, teh first one is called "Status", and it is DataGridViewCheckBoxColumn , as it was designed in the designer form - Visual Studio 2008 express , by draging the DataGridView and then editing the columns and the colums types.

Now, I succeeded to check all rows , by executing the following:

public void SelectAllCheckBoxes()
{
    for(int i=0 ; i<EmployeeDataGridView.Rows.Count ; i++)
    {
        EmployeeDataGridView[i,0].Value = true;
    }
}

Now, How can I delete from the DataGridView all teh checked rows, after pressing Delete Button??

Hope that someone can tell me.

I guess should be a fast way ..

Thank you



推荐答案

public partial class DataGrid : Form
   {
      public DataGrid()
      {
         InitializeComponent();
         this.dataGridView1.DataSource = this.GetDataTable();
      
      }

      private DataTable GetDataTable()
      {
         DataTable table = new DataTable();
         table.Columns.Add("test");
         table.Rows.Add("test1");
         table.Rows.Add("test2");
         return table;
      }

      private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
         
      }

      private void button1_Click(object sender, EventArgs e)
      {
         //delete all the selected rows
         foreach (DataGridViewRow row in this.dataGridView1.Rows)
         {
            DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;
            if (cell != null && (bool)cell.FormattedValue)
            {
               this.dataGridView1.Rows.Remove(row);
            }
         }
      }
   }


我刚才写了一个例子。
我希望你能做到从我的例子中得到一些帮助。

Hi,there is an example I wrote just now.
I hope you can get some help from my example.


这篇关于C#删除所有Checked DataGridViewCheckBoxColumn行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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