Results 1 to 3 of 3

Thread: [RESOLVED] Datagrids

  1. #1

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

    Resolved [RESOLVED] Datagrids

    Hi,
    First of all Im a newbie, and just trying to understand a little more about datagrids. At the moment so far, I have managed to update a datagrid, using arrays.

    My code is below.

    Basically I have some textboxes and a submit button. When I submit, the informacion in the textboxes appears in the "Datagrid".

    So far so good, no problems with this.

    All I want to understand is, what is happening, when Im selecting rows in the datagrid.

    I would like to do something simple like, have a button, and once click, will show a messge on screen, saying "This is row number" and the row number.




    Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim i As Integer = 0
    Dim dt As New DataTable()
    Structure gridTest

    Dim name As String
    Dim surname As String
    Dim address As String
    Dim Telephone As String

    End Structure

    Dim arrayTest() As gridTest


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

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    ReDim Preserve arrayTest(i)
    arrayTest(i).name = txtboxName.Text
    arrayTest(i).surname = txtboxSurname.Text
    arrayTest(i).address = txtboxAddress.Text
    arrayTest(i).Telephone = txtboxTelephone.Text

    addInfo()

    txtboxName.Clear()
    txtboxSurname.Clear()
    txtboxAddress.Clear()
    txtboxTelephone.Clear()

    i = i + 1

    End Sub

    Sub addInfo()

    Dim dr As DataRow = dt.NewRow
    dr("Number") = i.ToString
    dr("Names") = arrayTest(i).name
    dr("Surname") = arrayTest(i).surname
    dr("Address") = arrayTest(i).address
    dr("Telephone") = arrayTest(i).Telephone

    dt.Rows.Add(dr)

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

    Dim dr As DataRow


    dt.Rows.Remove(dr)

    End Sub
    End Class

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

    Re: Datagrids

    I don't quite understand what purpose your arrayTest is supposed to serve. All the data is in the DataTable so the array is superfluous.

    The index of the currently selected row in the DataGrid is available from the CurrentRowIndex property. Note that this is zero-based, like all collections and arrays in .NET. If you want the number of rows already in the grid then you'd use the Rows.Count property of the DataTable.

  3. #3

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

    Re: Datagrids

    Cheers, Thanks a lot.

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