Datagrid Check Box

I have developed a Windows Forms application and it has a datagrid with a checkbox. If I click a button, I want to get all the checked rows and perform some operation on them. The problem is that when the button is clicked, it doesn't recognize the last action of the checkbox. It's like a step back.

My code:

    private void copyToToolStripMenuItem_Click(object sender, EventArgs e)
    {
        dataGridView1.ClearSelection();
        DialogResult result = this.folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            string foldername = this.folderBrowserDialog1.SelectedPath;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    if ((bool)row.Cells[0].Value == true)
                    {
                        try
                        {
                            string[] vars = row.Cells[1].Value.ToString().Split('\\');
                            System.IO.File.Copy(row.Cells[1].Value.ToString(), foldername + @"\" + vars[vars.Length - 1], true);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                }
            }
        }
    }

      

Data taken from SQL query - common stuff.

+2


source to share


1 answer


Roland,

The reason may be that the data has not yet been transferred to the source. The way the DataGridView works is that it is "bound" to a source and synchronizes data. However, changes made to the DataGridView do not update the DataGridView immediately. This is considered "dirty".



Try to check the original datasource after change, you can also try calling BindingContext (..). EndCurrentEndit (); or something like trying and securing data transfer.

+1


source







All Articles