Select Multiples cells in Datagridview


Introduction:

In this article I will explain about how to select multiple cells in datgridview using c# windows application. Use the MultiSelect property to allow a user to select multiple elements in the DataGridView control and perform an operation on all the selected elements.

How to start?

1. First open the visual studio and select Visual C# --> Windows --> WindowsFormsApplication.

2. Create new item and add one Form.

3. Drag and Drop datagridview control and named as datagridview1.

4. Double Click the form and move code behind Form1_Load.

5. Just dynamically update the datagridview properties as below.

dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = true;

6. For modification manually in datagridview Propertise

DatagridviewPropertise.GIF

7. Bind the datasource to the datagridview.

dataGridView1.DataSource = dtTemp;

8. Dynamically select the rows through code behind itself.

dataGridView1[1,1].Selected = true;
dataGridView1[2, 1].Selected = true;
dataGridView1[0, 2].Selected = true;
dataGridView1[1, 2].Selected = true;

9. Run the form and result will be as below.

DatagridviewForm.GIF

Here you can change the selected cells based on the scenario. I just explained to you how to select the cells through code line.