Click here to Skip to main content
16,021,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created three datagridviews in form. All three gridviews datas are from only one table. One gridview contains (itmcod,title,procod,typcod,title2) these fields. Second gridview contains (typetitle,procod,typcod,title2). And the third gridview contains title2,typcod. All three gridviews have some unique fields (procod, typcod). If i select one value from datagridview2 (using checking the checkbox) automatically the other two gridviews which has the same procod and typcod value row should be selected with check box selecion.

What I have tried:

I've linked three tables into one temporary table and retreived datas to gridview from temptable. but i dont no how to use checkbox selection option for this
Posted
Updated 23-Jun-16 1:27am

1 solution

This turned out to be more fiddly than I expected!

Choosing the appropriate event to put code behind was the first problem. CellValidated or CellValueChanged were my first thoughts, but these were not being fired when I expected (for example CellValidated is not fired when the checkbox is clicked, but when you click elsewhere - not a good user experience)

I ended up choosing the dataGridView2_CellClick event, which brings it's own problems ... when checking the .Value of the cell on the first click you will find it is null. So I had to use EditingCellFormattedValue instead - trouble is, at the time the event is fired this value is still the opposite of what we think it is ... in other words when we are clicking the tick box this value is still false - it has the value we are moving from not the value we are moving to. I'm labouring this point a little to draw attention to the not (!) operator in the line
C#
var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;

The next issue to consider is whether or not the properties AllowUserToAddRows has been set to true or false (the default is true). In the code below I handle either, but if you omit the setting of this to false when using the foreach you will get an exception
Quote:
An unhandled exception of type 'System.NullReferenceException' occurred in Sandbox.exe

Additional information: Object reference not set to an instance of an object.


Here is my full solution
C#
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var r = e.RowIndex;
    var c = e.ColumnIndex;

    if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;

    var procodSearch = dataGridView2.Rows[r].Cells[2].Value.ToString();
    var typcodSearch = dataGridView2.Rows[r].Cells[3].Value.ToString();
    var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;

    var temp = dataGridView1.AllowUserToAddRows;
    dataGridView1.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView1.Rows)
    {
        if (dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
            && dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
            ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
    }
    dataGridView1.AllowUserToAddRows = temp;

    temp = dataGridView3.AllowUserToAddRows;
    dataGridView3.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView3.Rows)
    {
        if (dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
            ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
    }
    dataGridView3.AllowUserToAddRows = temp;

}

Or if you prefer to use Linq:
C#
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var r = e.RowIndex;
    var c = e.ColumnIndex;

    if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;

    var procodSearch = dataGridView2.Rows[r].Cells["procod"].Value.ToString();
    var typcodSearch = dataGridView2.Rows[r].Cells["typcod"].Value.ToString();
    var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;

    var temp = dataGridView1.AllowUserToAddRows;
    dataGridView1.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(dr => dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
           && dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
        ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;

    dataGridView1.AllowUserToAddRows = temp;

    temp = dataGridView3.AllowUserToAddRows;
    dataGridView3.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView3.Rows.Cast<DataGridViewRow>()
        .Where(dr => dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
        ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;

    dataGridView3.AllowUserToAddRows = temp;

}

This is not an ideal solution - for example I have made some assumptions about which columns contain the CheckBoxes (I've assumed column 0 above) and there is no checking for null values in any columns, but it's a start.
 
Share this answer
 
Comments
Mohamed Shakir 24-Jun-16 1:55am    
Its a nice solutions. Its working. Thank you Chill60. Awesome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900