Results 1 to 4 of 4

Thread: [RESOLVED] Add value selected row Datagrid

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Resolved [RESOLVED] Add value selected row Datagrid

    how to add value to selected row at datagridview control.
    hire my code to display and add row at datagrid :
    vb.net Code:
    1. Public Class Form2
    2.     Dim dt As New DataTable("view")
    3.  
    4.     Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    5.         dt.Columns.Add(New DataColumn("Col1"))
    6.         dt.Columns.Add(New DataColumn("Col2"))
    7.        
    8.  
    9.         dg.DataMember = "view"
    10.         dg.DataSource = dt
    11.  
    12.  
    13.     End Sub
    14.  
    15.     'new rows
    16.     Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
    17.         Dim dr As DataRow
    18.         dr = dt.NewRow
    19.         dr.Item(0) = ""
    20.         dr.Item(1) = ""
    21.         dt.Rows.Add(dr)
    22.  
    23.     End Sub
    24.  
    25.     'add value at selected row
    26.     Private Sub addValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addValue.Click
    27.         ' ???
    28.  
    29.     End Sub
    30. End Class

    thank you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add value selected row Datagrid

    First up, you shouldn't be setting the DataMember. You would only set the DataMember to the name of a DataTable if you were setting the DataSource using a DataSet containing that DataTable. Your DataTable is the DataSource so there is no DataMember.

    As for the question, you should bind your table to the grid via a BindingSource. That way, you can use the Current property of the BindingSource to get a DataRowView for the current row. You then set it's fields by name or index:
    vb.net Code:
    1. Me.BindingSource1.DataSource = dt
    2. dg.DataSource = Me.BindingSource1
    vb.net Code:
    1. Dim row = DirectCast(Me.BindingSource1.Current, DataRowView)
    2.  
    3. row(0) = firstColumnValue
    4. row("ColumnName") = namedColumnValue
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Add value selected row Datagrid

    Thank you code can run with either
    But what if the datagrid I is not connected to the database?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add value selected row Datagrid

    There's no such thing as a control connected to a database. Controls are bound to a list of data. That list might be a DataTable or it might be something else. Even if it's a DataTable, where the data in that table came from is irrelevant to the control. It might be a database or it might not, but a DataTable is a DataTable. If you are binding any list to the grid then do so via a BindingSource and get the current item from the Current property. The type of that object will depend on the type of data you bound in the first place.

    For a grid that isn't bound, which would be the exception rather than the rule, you can use the grid's CurrentRow property. That assumes that the control is actually a DataGridView rather than a DataGrid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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