Click here to Skip to main content
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)

  Print Answers RSS
0 OriginalGriff 373
1 Sergey Alexandrovich Kryukov 197
2 Richard Deeming 160
3 PhilLenoir 120
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530

Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
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