Click here to Skip to main content
11,642,381 members (67,306 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
i have a windows form in which we have a datagridview. the property of that is cell select. and i have a contextmenustrip in which there is a menu named select all when select all is clicked it should change the property of datagridview **of the selected cell** to the full row select and the selection should be on the same row which i have clicked. the problem is when i click on a cell the default property is cell select and when i click on select all of the context menustrip the selected cell is not selected and i have to reselect that row i want that when the form opens and when i click on particular cell and when i click on select all of the contextmenustrip is clicked then the same row should be selected on which i have clicked the cell previously
this is my code

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
       dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
Posted 12-Jan-13 23:14pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is no automatic way to do it - indeed there is little but the Brute Force and Ignorance approach either! Laugh | :laugh:
This is a dummy which does what you want: If you click the Right button, it sets Row select, the middle button sets cell select back.
         private void dgvNames_MouseClick(object sender, MouseEventArgs e)
             DataGridView dgv = sender as DataGridView;
             if (dgv != null)
                 if (e.Button == MouseButtons.Right)
                     DataGridViewSelectedCellCollection cells = dgv.SelectedCells;
                     dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                     foreach (DataGridViewCell cell in cells)
                         DataGridViewRow row = dgv.Rows[cell.RowIndex];
                         row.Selected = true;
                 else if (e.Button == MouseButtons.Middle)
                     DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
                     dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
                     foreach (DataGridViewRow row in rows)
                         DataGridViewCell cell = row.Cells[0];
                         cell.Selected = true;
shaikh-adil at 13-Jan-13 8:03am
thank you sir,
thank yo u very much for helping
OriginalGriff at 13-Jan-13 8:08am
You're welcome!
shaikh-adil at 13-Jan-13 8:12am
cam you help me once more sir? may i ask another question? are you free now?
OriginalGriff at 13-Jan-13 8:21am
You can ask, but I have to go out for a while, so I can't tell you when I can answer!
shaikh-adil at 13-Jan-13 8:32am
okay sir help me if you get my comment

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

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100