jeudi 24 mars 2016

C# how to keep Checkbox checked of DataGridView when reload DataGridView

My application contains DataGridView with Checkbox added inside. How can I keep Checkbox checked when perform search and return new result in DataGridView.

    //List<DataGridViewRow> lst;
    List<string> lst;
    private void button1_Click(object sender, EventArgs e)
    {
        lst = new List<string>();
        foreach (DataGridViewRow row in gwCustomer.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;
        gwCustomer.DataSource = rs.ToList();

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




Aucun commentaire:

Enregistrer un commentaire