Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

using datagrid to input data in VB.net 2005

yaosmanyaosman Posts: 2Member
Hi all,

I need help. I want to use a datagrid to capture data in VB.Net in unbound mode using code.

My table is

Employee
--------
EmpCode
EmpName
Salary
Home

The form should have an ADD button, when clicked it should commit the data.
Cheers



Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Ok this should be fairly straight forward. I am not going to write the code straight out for you, but I will show you peices of code you will need and explain them so you understand what to do. That way you get benifit of writing / understanding it yourself incase this is a school project or work project.

    I am under the impression that you always have to bind a DataGrid. However, this does not meen it has to be bound to a Database, just bind it to a DataTable object.

    To start off, define a Private Global Varliable to hold your Data in.
    [code]
    'This will reside after the Windows Define Code Area
    'but will be outside of any functions
    'Since I usually keep my Load function at the top, I
    'typically define these right above my load function

    Dim DT as DataTable
    [/code]

    Be sure to initialize this Object with a "New" statement in your Form_Load routine.
    [code]
    DT = new DataTable
    [/code]

    Now we can play with the object. I usually add Columns at this point, because they will need to be in place to add any new items. If you want a really robust tool, you would make a routine to configure those columns and do checks for DT.Columns.Count to see if it's set up, but you probably can just do this in your FORM_LOAD routine for your purposes.

    [code]
    DT.Columns.Add("Field1")
    DT.Columns.Add("Field2")
    DT.Columns.Add("Field3")
    DT.Columns.Add("Field4")
    [/code]

    Now. Bind this DataTable object to the DataGrid. Binding acts like a pointer reference. One you bind DT to the DataGrid's DataSource, any further changes to DT will appear in the DataGrid (essentially, all work you do will be with the DT object after you bind).

    [code]
    DataGrid1.DataSource = DT
    DataGrid1.Refresh() 'I dont know if you need this, but it was in my code so...
    [/code]

    Ok. Lets add a row and see this bad boy work. To do that, create a button on the form to call code to add a row to the DT object.

    [code]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim R As DataRow = DT.NewRow
    R.Item(0) = "testcode" 'field1
    R.Item(1) = "moretest" 'field2
    R.Item(2) = "ok" 'field3
    R.Item(3) = "field4" 'field4

    DT.Rows.Add(R)
    End Sub
    [/code]

    And there you have it... That is how you can create a DataGrid and have it populate with data that is not from a database table. So you can write functions to skim data off of the TextBox or Other objects to add new rows to the datatable. I hope this helps you!!
    -Firesickle.com
Sign In or Register to comment.