using datagrid to input data in VB.net 2005 - Programmers Heaven
It looks like you're new here. If you want to get involved, click one of these buttons!
C and C++
Delphi and Kylix
DirectX Game dev
Newbie Game Programmers
Database & SQL
Sound & Music
FreeLance Software City
Join the Team
Comments on this site
New programming languages
Off topic board
Mobile & Wireless
Operating Systems & Platforms
Embedded / RTOS
Windows CE & Pocket PC
Quality & Testing
Active Server Pages
HTML & WEB-Design
WEB-Services / SOAP
using datagrid to input data in VB.net 2005
I need help. I want to use a datagrid to capture data in VB.Net in unbound mode using code.
My table is
The form should have an ADD button, when clicked it should commit the data.
Share on Facebook
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.
'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
Be sure to initialize this Object with a "New" statement in your Form_Load routine.
DT = new DataTable
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.
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).
DataGrid1.DataSource = DT
DataGrid1.Refresh() 'I dont know if you need this, but it was in my code so...
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.
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
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!!
Share on Facebook
Programmers Heaven articles
Programmers Heaven files
Programmers Heaven uploaded content
Programmers Heaven C Sharp ebook
Operated by CommunityHeaven LLC
- All rights reserved.