Results 1 to 4 of 4

Thread: DGV Selected Cells Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    142

    Question DGV Selected Cells Problem

    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:
    1. For i As Integer = 0 To Me.DataGridView1.CurrentRow.Cells.Count - 1
    2.             If Me.DataGridView1.CurrentRow.Cells(i).Selected = True Then Exit Sub
    3.         Next

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: DGV Selected Cells Problem

    Quote Originally Posted by mangore View Post
    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:
    1. For i As Integer = 0 To Me.DataGridView1.CurrentRow.Cells.Count - 1
    2.             If Me.DataGridView1.CurrentRow.Cells(i).Selected = True Then Exit Sub
    3.         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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    142

    Re: DGV Selected Cells Problem

    I'm confusing about this code. Because I'm new in vb.net

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: DGV Selected Cells Problem

    Quote Originally Posted by mangore View Post
    I'm confusing about this code. Because I'm new in vb.net
    What I gave you is per-say advance but it should not stop you from trying it which once tried will show results in the IDE output window thus seeing what it did. From there you can ask questions rather than simply saying "I'm confused."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width