Results 1 to 4 of 4

Thread: newbie-with a newbie question,(datagrids)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    newbie-with a newbie question,(datagrids)

    Hi,

    I just want to add information to the column of a data grid.

    The way I want to do this is by having a text field on my form and a button, what ever you write in the text field appears in the first column of the data grid, then you write some more text in the text box, hit the button and appears on the second row, well this is what I want to achive!

    So far I have this code: But it gives me an error regarding the column. It says it doesn't know under which column it needs to add the information to.

    How can I tell VB.net, to always input the information under the same column?? In this case "Names"

    Dim dt As New DataTable
    Dim f As Integer = 0
    Dim s() As String


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    dt.Columns.Add("Names")
    Me.dtdgrd1.DataSource = dt
    End Sub

    Sub values()

    (I have got to some how make a reference to column one, "Names", but do not know how to do it)

    Dim dr As DataRow = dt.NewRow
    dr(f) = s(f).ToString
    dt.Rows.Add(dr)


    End Sub

    Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click

    ReDim Preserve s(f)
    s(f) = txtbox1.Text
    values()
    f = f + 1

    End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,454

    Re: newbie-with a newbie question,(datagrids)

    In the Click event handler of the Button, create a new row, set its Name column, then add it to the DataTable. It will automatically appear in the grid:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'Get the table that is bound to the grid.
    3.         Dim dt As DataTable = DirectCast(Me.DataGrid1.DataSource, DataTable)
    4.  
    5.         'Create a new row.
    6.         Dim dr As DataRow = dt.NewRow()
    7.  
    8.         'Set the Name column to the specified value.
    9.         dr("Name") = Me.TextBox1.Text
    10.  
    11.         'Add the row to the table.
    12.         dt.Rows.Add(dr)
    13.     End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: newbie-with a newbie question,(datagrids)

    Thanks for that,
    I realised the bit I had incorrect,...

    Dim dr As DataRow = dt.NewRow
    dr(f) = s(f).ToString
    dt.Rows.Add(dr)

    should of been,....

    Dim dr As DataRow = dt.NewRow
    dr("Names") = s(f).ToString
    dt.Rows.Add(dr)

    Cheers!!
    Rauland

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,454

    Re: newbie-with a newbie question,(datagrids)

    Cool. Don't forget to resolve your thread from the Thread Tools menu.

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