question

jumexmango-8448 avatar image
0 Votes"
jumexmango-8448 asked XingyuZhao-MSFT commented

How can I programmatically select every newly added row in a DataGridView?

I have a button set up that adds a new row to my data grid.

I have tried
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0)

While this code does select every newly added row, it resets all my True's to False (I have check boxes that allow the user to change the columns between true and false). This only happens when this code is implemented.

EDIT: I just found out that it is only resenting the row previous to the newly added one.


dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @jumexmango-8448 ,
Could you provide more related code here? It will help us make a test and reproduce your problem.
Generally, you use the following code to select the new row.

     Private rowId As Integer
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         rowId = DataGridView1.Rows.Add()
     End Sub
    
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
         DataGridView1.Rows(rowId).Selected = True
         'DataGridView1.Rows(rowId).Cells(...) = ...
     End Sub



0 Votes 0 ·

Here is more related code:

     Private Sub btncookie_Click(sender As Object, e As EventArgs) Handles btncookie.Click
         For Each row As DataGridViewRow In DataGridView1.Rows
             If row.Cells(0).Value = "cookie" Then
                 row.Cells(1).Value = Double.Parse(row.Cells(1).Value)
                 row.Cells(2).Value = Double.Parse(row.Cells(2).Value)
             End If
         Next
        
         Dim rowId As Integer
         rowId = DataGridView1.Rows.Add("cookie", "1", 3.5, True, True, True, True, True, True, True, True)
         DataGridView1.CurrentCell = DataGridView1.Rows(rowId).Cells(0)
         'DataGridView1.ClearSelection()
        
     Addcost()
        
    End Sub








0 Votes 0 ·
XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered XingyuZhao-MSFT commented

Hi @jumexmango-8448 ,
I make a test on my side, and here's the result of my test.
140404-gif.gif

It seems that it does select every newly added row.
Code of my test.

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         DataGridView1.Rows.Add("row1", "2", 3.5, False, False)
         DataGridView1.Rows.Add("cookie", "1", 4, False, True)
         DataGridView1.Rows.Add("row2", "3", 4.5, True, False)
         DataGridView1.Rows.Add("cookie", "1", 5.5, False, True)
     End Sub
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         For Each row As DataGridViewRow In DataGridView1.Rows
             If row.Cells(0).Value = "cookie" Then
                 row.Cells(1).Value = Double.Parse(row.Cells(1).Value)
                 row.Cells(2).Value = Double.Parse(row.Cells(2).Value)
             End If
         Next
    
         Dim rowId As Integer
         rowId = DataGridView1.Rows.Add("cookie", "1", 3.5, True, True)
         DataGridView1.CurrentCell = DataGridView1.Rows(rowId).Cells(0)
     End Sub

Please let me know if I have any misunderstanding.



gif.gif (29.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes this does work! The issue ended up being a discrepancy with another Private Sub, thank you.

0 Votes 0 ·

Hi @jumexmango-8448 ,
I have converted my comment to answer, and if the answer is helpful, please click "Accept Answer" and upvote it.

1 Vote 1 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

You can do :

 bSelect = True
 ' Add your row here 

then

     Private bSelect As Boolean = False

     Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
         If (bSelect) Then
             DataGridView1.Rows(e.RowIndex).Selected = True
             bSelect = False
         End If
     End Sub




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.