| True DBGrid for WinForms Task-Based Help > Adding a New Row to C1TrueDBGrid > Accessing the Values of the Selected Rows in the Grid |
To access the values of the selected rows in a grid, you must access the SelectedRows collection.
Use the following code to write each of the selected rows to the Debug window:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim row As Integer
For Each row In Me.C1TrueDBGrid1.SelectedRows
Debug.WriteLine(Me.C1TrueDBGrid1.Columns(0).CellValue(row))
Next
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
int row;
foreach (int row in this.c1TrueDBGrid1.SelectedRows)
{
Debug.WriteLine(this.c1TrueDBGrid1.Columns(0).CellValue(row));
}
|
|
You can also use the grid's index to access the rows. Use the following code:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim row As Integer
For Each row In Me.C1TrueDBGrid1.SelectedRows
Debug.WriteLine(Me.C1TrueDBGrid1(row, 0).ToString())
Next
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
int row;
foreach (int row in this.c1TrueDBGrid1.SelectedRows)
{
Debug.WriteLine(this.c1TrueDBGrid1(row, 0).ToString());
}
|
|
For this example, the following code was added to the Button1_Click event in Tutorial 5: Selecting Multiple Rows Using Bookmarks:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim row As Integer
For Each row In Me.C1TrueDBGrid1.SelectedRows
Debug.WriteLine(Me.C1TrueDBGrid1(row, 1).ToString())
Next
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
int row;
foreach (int row in this.c1TrueDBGrid1.SelectedRows)
{
Debug.WriteLine(this.c1TrueDBGrid1(row, 1).ToString());
}
|
|
The Last name of each composer in a selected row is returned in the Debug window: