Hi,

I have a panel which contains numerous dynamically generated datagridviews. Everything is working pretty fine, but for instance if there are three datagridviews in that panel. At least one row is selected as default in each datagridview, So what I want is to get colored only user selected row (which is usually colored blue) and the rest of the rows in all the datagridviews remain unselected.

How can I do that?

Please help,

Thanks

Recommended Answers

All 10 Replies

Hi,

Did you try
DataGridViewInstance.Select(aRowToBeSelected)
DataGridViewInstance.SelectionBackColor = Color.Blue

Regards,
Tony

This might help:

dataGridView1.Rows[0].Cells[0].Selected = false;

Hi,

Sorry my bad that I didn't post the code before itself.

It is how the dynamic datagridview generation works. So I guess if we click, the event is limited to that gridview which is selected so the highlited row in other gridviews remains which I don't want to happen

for (int i = 0; i <= distinctSystemIDs.Rows.Count - 1; i++)
            {

               
                int systemID = (int)distinctSystemIDs.Rows[i][0];
            //    MessageBox.Show(systemID.ToString());
                textBox260.Text = systemID.ToString();
                int g = 0;
                int y = 2;
                DataGridView dgw = new DataGridView();
                Label l = new Label();
                l.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                l.Location = pt;
                DataSet z14 = new DataSet();
               
               
                dgw.Tag = systemID; // store systemID of the DGV if you need it later
               
               
               
                dgw.Location = pt;

                pt.Y += dgw.Height + margin+2; // margin is for spacing between grids
                panel12.Controls.Add(dgw);
                panel12.Controls.Add(l);
                DataView vw = new DataView(dt1, string.Format("c_AnalyticalSystemID = {0}", systemID), null, DataViewRowState.CurrentRows);
                dgw.DataSource = vw;

               
                dgw.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgw.CellMouseClick += new DataGridViewCellMouseEventHandler(dgw_CellMouseClick);
              //  dgw.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
               
            }
 private void dgw_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           
            DataGridView dgw = sender as DataGridView;
            // dgw.ClearSelection();
            
           
            try
            {

                
                DataGridViewRow row = dgw.Rows[e.RowIndex];
                textBox77.Text = row.Cells[0].Value.ToString();
                textBox78.Text = row.Cells[0].Value.ToString();
                
            }
            catch (Exception ex)
            {
                
            }

        }

This will do to deselect all:

protected override void OnShown(EventArgs e)
        {
            dataGridView1[0, 0].Selected = false;
            dataGridView2[0, 0].Selected = false;
            dataGridView3[0, 0].Selected = false;
        }

This will do to deselect all:

protected override void OnShown(EventArgs e)
        {
            dataGridView1[0, 0].Selected = false;
            dataGridView2[0, 0].Selected = false;
            dataGridView3[0, 0].Selected = false;
        }

Hi,

Actually there are no dataGridView1,2,3. All the gridviews have the name dgw that's the problem.

one wild guess what I have got is to count the number of datagrid views in that panel and then deselect all and remains the latest one which is selected by the user but I have no idea how to do it.

Why you dont name the differently? This is a stupid idea. And i dont even know how your code works.
but anyway, you can loop through them and do:

protected override void OnShown(EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (c is DataGridView)
                {
                    DataGridView dgv = c as DataGridView;
                    dgv[0, 0].Selected = false;
                }
            }
        }

Why you dont name the differently? This is a stupid idea. And i dont even know how your code works.
but anyway, you can loop through them and do:

protected override void OnShown(EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (c is DataGridView)
                {
                    DataGridView dgv = c as DataGridView;
                    dgv[0, 0].Selected = false;
                }
            }
        }

Thanks it is working pretty good but the thing is it is removing the current selected row. I kept the code in dgv_mouseclick event. IS it the right place where I have to keep it?

Keep what?
I actually didnt understand you well. Did you select any cells (or rows) before calling OnShown method?

Keep what?
I actually didnt understand you well. Did you select any cells (or rows) before calling OnShown method?

Yes I selected a row in a datagridview and called the OnShown method, then all the rows including the latest selected row also disappeared.

Keep what?
I actually didnt understand you well. Did you select any cells (or rows) before calling OnShown method?

HI Mitja ,

I have done it

thanks a lot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.