
Originally Posted by
mangore
I created this code to work if I select Cell in DataGridView. The problem That I faced is when I select the Row it work too(because it sonsider the cells as selected).
How can I make it doesn't work for Row only for Cells as individual ?
vb Code:
For i As Integer = 0 To Me.DataGridView1.CurrentRow.Cells.Count - 1
If Me.DataGridView1.CurrentRow.Cells(i).Selected = True Then Exit Sub
Next
If you select the entire row then SelectedCells will contain each cell.
If you had not selected the entire row but only one or more columns, less than the total columns you could use code such as the following to see which columns are selected for the current row that is not selected with the row selector (which would return all columns on the current row). The code assumes your DataGridView has allow new rows set to false.
Code:
If DataGridView1.SelectedCells IsNot Nothing Then
Dim Data1 = (From D In DataGridView1.SelectedCells.Cast(Of DataGridViewCell)() _
Where D.RowIndex = DataGridView1.CurrentRow.Index _
Select D Order By D.ColumnIndex).ToList
For i As Integer = 0 To Data1.Count - 1
Console.WriteLine(Data1(i).ColumnIndex)
Next
End If