Results 1 to 3 of 3

Thread: Update selected row in datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    35

    Update selected row in datagridview

    I want to have a button onces clicked up date a selected row field in my datagridview to either check a checkbox or set the field to lets say complete.

    I tried using the following but I get a error. ( I am totally guessing here on this line of code! I bet that is why it does work LOL.)
    Kb_cartDataGridView.CurrentRow.Selected = "status='" & True & "'"

    Error:
    Conversion from string "status='True'" to type 'Boolean' is not valid.

    Thanks

    Richard

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    35

    Re: Update selected row in datagridview

    Okay I figured out that:
    Kb_cartDataGridView.CurrentRow.Selected = "status='" & True & "'"

    Is not what I wanted... as it seems this code is to select the entire row.. and I had it wrote wrong.
    Kb_cartDataGridView.CurrentRow.Selected = True

    So now I have the whole row highlighted how do I update complete field to completed?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Update selected row in datagridview

    The MSDN documentation says of the DataGridViewRow.Selected property:
    Gets or sets a value indicating whether the row is selected.
    Is that actually what you want to do, set whether the row is selected or not? Obviously not, as you say that you want to set the value of a field in that row.

    A quick look at the MSDN help topic for the DataGridViewRow member listing shows the Cells property, described thusly:
    Gets the collection of cells that populate the row.
    Hmmm... that sounds like it might be useful, given that you want to set the value of a cell in that row.

    Clicking on the property name, which is a link, takes us to the Help topic for that property. It has an Example section that says this:
    The following code example uses the Cells property to set the value of a cell in the row. This code example is part of a larger code example provided in How to: Manipulate Rows in the Windows Forms DataGridView Control.
    and provides this code example:
    Code:
    ' Give cheescake excellent rating.
    Private Sub Button8_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button8.Click
    
        UpdateStars(dataGridView.Rows(4), "******************")
    End Sub
    
    Private ratingColumn As Integer = 3
    
    Private Sub UpdateStars(ByVal row As DataGridViewRow, _
        ByVal stars As String)
    
        row.Cells(ratingColumn).Value = stars
    
        ' Resize the column width to account for the new value.
        row.DataGridView.AutoResizeColumn(ratingColumn, _
            DataGridViewAutoSizeColumnMode.DisplayedCells)
    
    End Sub
    Who would have thought the Help could be so helpful? Perhaps it could even help someone without a comprehensive understanding of VB to gain that understanding. It might even help them learn things that they weren't even looking for.

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