Training
Learning path
Work with variable data in C# console applications (Get started with C#, Part 4) - Training
Work with variable data in C# console applications (Get started with C#, Part 4)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
You can get the selected cells, rows, or columns from a DataGridView control by using the corresponding properties: SelectedCells, SelectedRows, and SelectedColumns. In the following procedures, you will get the selected cells and display their row and column indexes in a MessageBox.
Use the SelectedCells property.
Note
Use the AreAllCellsSelected method to avoid showing a potentially large number of cells.
private void selectedCellsButton_Click(object sender, System.EventArgs e)
{
Int32 selectedCellCount =
dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
if (dataGridView1.AreAllCellsSelected(true))
{
MessageBox.Show("All cells are selected", "Selected Cells");
}
else
{
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
for (int i = 0;
i < selectedCellCount; i++)
{
sb.Append("Row: ");
sb.Append(dataGridView1.SelectedCells[i].RowIndex
.ToString());
sb.Append(", Column: ");
sb.Append(dataGridView1.SelectedCells[i].ColumnIndex
.ToString());
sb.Append(Environment.NewLine);
}
sb.Append("Total: " + selectedCellCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Cells");
}
}
}
Private Sub selectedCellsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedCellsButton.Click
Dim selectedCellCount As Integer = _
dataGridView1.GetCellCount(DataGridViewElementStates.Selected)
If selectedCellCount > 0 Then
If dataGridView1.AreAllCellsSelected(True) Then
MessageBox.Show("All cells are selected", "Selected Cells")
Else
Dim sb As New System.Text.StringBuilder()
Dim i As Integer
For i = 0 To selectedCellCount - 1
sb.Append("Row: ")
sb.Append(dataGridView1.SelectedCells(i).RowIndex _
.ToString())
sb.Append(", Column: ")
sb.Append(dataGridView1.SelectedCells(i).ColumnIndex _
.ToString())
sb.Append(Environment.NewLine)
Next i
sb.Append("Total: " + selectedCellCount.ToString())
MessageBox.Show(sb.ToString(), "Selected Cells")
End If
End If
End Sub
Use the SelectedRows property. To enable users to select rows, you must set the SelectionMode property to FullRowSelect or RowHeaderSelect.
private void selectedRowsButton_Click(object sender, System.EventArgs e)
{
Int32 selectedRowCount =
dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
if (selectedRowCount > 0)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < selectedRowCount; i++)
{
sb.Append("Row: ");
sb.Append(dataGridView1.SelectedRows[i].Index.ToString());
sb.Append(Environment.NewLine);
}
sb.Append("Total: " + selectedRowCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Rows");
}
}
Private Sub selectedRowsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedRowsButton.Click
Dim selectedRowCount As Integer = _
dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
If selectedRowCount > 0 Then
Dim sb As New System.Text.StringBuilder()
Dim i As Integer
For i = 0 To selectedRowCount - 1
sb.Append("Row: ")
sb.Append(dataGridView1.SelectedRows(i).Index.ToString())
sb.Append(Environment.NewLine)
Next i
sb.Append("Total: " + selectedRowCount.ToString())
MessageBox.Show(sb.ToString(), "Selected Rows")
End If
End Sub
Use the SelectedColumns property. To enable users to select columns, you must set the SelectionMode property to FullColumnSelect or ColumnHeaderSelect.
private void selectedColumnsButton_Click(object sender, System.EventArgs e)
{
Int32 selectedColumnCount = dataGridView1.Columns
.GetColumnCount(DataGridViewElementStates.Selected);
if (selectedColumnCount > 0)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < selectedColumnCount; i++)
{
sb.Append("Column: ");
sb.Append(dataGridView1.SelectedColumns[i].Index
.ToString());
sb.Append(Environment.NewLine);
}
sb.Append("Total: " + selectedColumnCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Columns");
}
}
Private Sub selectedColumnsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedColumnsButton.Click
Dim selectedColumnCount As Integer = dataGridView1.Columns _
.GetColumnCount(DataGridViewElementStates.Selected)
If selectedColumnCount > 0 Then
Dim sb As New System.Text.StringBuilder()
Dim i As Integer
For i = 0 To selectedColumnCount - 1
sb.Append("Column: ")
sb.Append(dataGridView1.SelectedColumns(i).Index.ToString())
sb.Append(Environment.NewLine)
Next i
sb.Append("Total: " + selectedColumnCount.ToString())
MessageBox.Show(sb.ToString(), "Selected Columns")
End If
End Sub
This example requires:
Button controls named selectedCellsButton
, selectedRowsButton
, and selectedColumnsButton
, each with handlers for the Click event attached.
A DataGridView control named dataGridView1
.
References to the System, System.Windows.Forms, and System.Text assemblies.
The collections described in this topic do not perform efficiently when large numbers of cells, rows, or columns are selected. For more information about using these collections with large amounts of data, see Best Practices for Scaling the Windows Forms DataGridView Control.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Training
Learning path
Work with variable data in C# console applications (Get started with C#, Part 4) - Training
Work with variable data in C# console applications (Get started with C#, Part 4)