Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
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;

1 solution

There is no automatic way to do it - indeed there is little but the Brute Force and Ignorance approach either! :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;
Share this answer
shaikh-adil 13-Jan-13 8:03am    
thank you sir,
thank yo u very much for helping
OriginalGriff 13-Jan-13 8:08am    
You're welcome!
shaikh-adil 13-Jan-13 8:12am    
cam you help me once more sir? may i ask another question? are you free now?
OriginalGriff 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 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)

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