Winform: Two different datagridview rows getting selected when one should be selected

Sudip Bhatt 2,271 Reputation points
2020-11-23T13:18:58.047+00:00

I am working with winform project. i have two datagridview which has same number of columns and same structure. frist column is checkbox column.

here is code to bind two datagrid with same kind of data.

List<AllGroupNames> grpDtl = GetAllGroups(Nodes);
List<AllGroupNames> grpDtl1 = GetAllGroups(Nodes); //grpDtl.GetRange(0, grpDtl.Count);

//bind two grid with all groups name
if (_grpDtl != null && _grpDtl.Count > 0)
{
    dgSingleGroups.AutoGenerateColumns = false;
    dgSingleGroups.DataSource = grpDtl;
    dgSingleGroups.Columns[0].DataPropertyName = "Select";
    dgSingleGroups.Columns[1].DataPropertyName = "GroupName";
    dgSingleGroups.Columns[1].ReadOnly = true;
    dgSingleGroups.Columns[0].Width = 47;
    dgSingleGroups.Columns[1].Width = 346;

    dgAllGroups.AutoGenerateColumns = false;
    dgAllGroups.DataSource = grpDtl1;
    dgAllGroups.Columns[0].DataPropertyName = "Select";
    dgAllGroups.Columns[1].DataPropertyName = "GroupName";
    dgAllGroups.Columns[1].ReadOnly = true;
    dgAllGroups.Columns[0].Width = 47;
    dgAllGroups.Columns[1].Width = 346;
}

grpDtl1 = null;
grpDtl = null;
_grpDtl = null;

GetAllGroups() iterate in treeview node collection and accumulate node name.

private List<AllGroupNames> GetAllGroups(TreeNodeCollection tnCollection)
        {
            //accumulate group name in recursive fashion
            foreach (TreeNode tn in tnCollection)
            {
                if (tn.Tag != null)
                {
                    if (((object)tn.Tag).GetType().ToString().Contains("TunerDetails"))
                    {
                        _grpDtl.Add(new AllGroupNames { Select = false, GroupName = tn.Text });
                    }
                    GetAllGroups(tn.Nodes);
                }
            }
            return _grpDtl;
        }

Now problem is when i check second grid checkbox then my first grid checkbox is getting checked invisibly means when i am reading first grid's first column value in loop then i am getting checkbox value true. where as i have not checked my first grid's any checkbox.

when i select any row of second grid that same row is getting automatically selected in first grid.
Jh4lg.png

why it is happening i am not able to capture the reason. please help me what to change in code to get rid of this problem.

I just select one row from right side grid and same row of left side grid automatically gets selected....which is problem for me. Why two grid syncing automatically. screen shot attached.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,815 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sudip Bhatt 2,271 Reputation points
    2020-11-23T15:54:11.233+00:00

    This problem solved. i got the clue why i was getting this error because same datasource has been used for two grid which causes the problem. now it is solved the moment i create two different datasource.

    List<...> data1 = ...;                      // shouldn't this be an BindingList<...> ?
    Dgv1.DataSource = data1;  
    List<...> data2 = new List<...>(data1);     // clone data1
    Dgv2.DataSource = data2;
    
    0 comments No comments

0 additional answers

Sort by: Most helpful