C#重新加载DataGridView时如何保持Checkbox检查DataGridView

时间:2016-03-24 15:14:31

标签: c# winforms checkbox datagridview

我的应用程序包含DataGridView,里面添加了Checkbox。如何在执行搜索时检查Checkbox并在DataGridView中返回新结果。

    //List<DataGridViewRow> lst;
    List<string> lst;
    THEGATESEntities ge = new THEGATESEntities();
    DataGridViewCheckBoxColumn checkboxColSelect;
    private void button1_Click(object sender, EventArgs e)
    {
        lst = new List<string>();
        foreach (DataGridViewRow row in gridviewStaffs.Rows)
        {
            if (Convert.ToBoolean(row.Cells[checkboxColSelect.Name].Value) == true)
            {
                MessageBox.Show(row.Cells[2].Value.ToString());
                lst.Add(row.Cells[2].Value.ToString());
            }
        }
    }

    private void txtName_TextChanged(object sender, EventArgs e)
    {
        var rs = from x in ge.Staffs
                 where x.username.Trim().ToLower().Contains(txtName.Text.Trim().ToLower())
                 select x;
        gridviewStaffs.DataSource = rs.ToList();

        foreach (DataGridViewRow row in gridviewStaffs.Rows)
        {
            foreach (string uname in lst)
            {
                if (Convert.ToString(row.Cells[2].Value) == uname)
                {
                    row.Cells[checkboxColSelect.Name].Value = 1;
                }
            }
        }
    }

0 个答案:

没有答案