saving data in datagridview

baxterbaxter

New member
Joined
Apr 13, 2006
Messages
2
Programming Experience
Beginner
I am new at programming the .net but trying to write a simple payroll program that uses a 'datagridview' grid to accept data from the user, such as employee names and hours worked, etc.
Problem is that I can not figure out how to save the data entered like one could with 'Excel' and be able to recall the data later and print it, but all from within my program.
thank you.
 
You can make a Datatable with the same collums from the datagrid u made then fill the datatable with the values from the datagrid and then save the datatable to an XML file with the WriteXML Method so that you can recall it later

VB.NET:
Dim gridtable As DataTable = New DataTable(datagridtable")
Dim gridtable_collumn1 As DataColumn = New _ DataColumn("datagridcollumn1")
Dim gridtable_collumn2 As DataColumn = New _ DataColumn("datagridcollumn2")

 gridtable.Columns.Add(gridtable_collumn1)
 gridtable.Columns.Add(gridtable_collumn2)

 Dim gridrow As DataGridViewRow
 Dim table_row As DataRow

  For Each gridrow In datagridview1.Rows
    table_row = gridtable .NewRow
     table_row ("datagridcollumn1")=gridrow.Cells("collumn1").Value
     table_row ("datagridcollumn2")=gridrow.Cells("collumn2").Value
     gridtable.Rows.Add( table_row)
  gridtable.WriteXml("test.xml")
next gridrow
Collumn1 and collumn2 are the names of the datagridview collumns and datagridcollumn1 and datagridcollumn2 are the names of the datatable.
Use the FOR EACH section of the sample code to a button to write the data to the XML and then to another button rebind the datagrid to the table(gridtable) to view the saved data.
 
You can make a Datatable with the same collums from the datagrid u made then fill the datatable with the values from the datagrid and then save the datatable to an XML file with the WriteXML Method so that you can recall it later

VB.NET:
Dim gridtable As DataTable = New DataTable(datagridtable")
Dim gridtable_collumn1 As DataColumn = New _ DataColumn("datagridcollumn1")
Dim gridtable_collumn2 As DataColumn = New _ DataColumn("datagridcollumn2")

 gridtable.Columns.Add(gridtable_collumn1)
 gridtable.Columns.Add(gridtable_collumn2)

 Dim gridrow As DataGridViewRow
 Dim table_row As DataRow

  For Each gridrow In datagridview1.Rows
    table_row = gridtable .NewRow
     table_row ("datagridcollumn1")=gridrow.Cells("collumn1").Value
     table_row ("datagridcollumn2")=gridrow.Cells("collumn2").Value
     gridtable.Rows.Add( table_row)
  gridtable.WriteXml("test.xml")
next gridrow
Collumn1 and collumn2 are the names of the datagridview collumns and datagridcollumn1 and datagridcollumn2 are the names of the datatable.
Use the FOR EACH section of the sample code to a button to write the data to the XML and then to another button rebind the datagrid to the table(gridtable) to view the saved data.

May I please have some assistance to get this code working in VB2010?
When I try to change (gridtable_column1) to the actual name of a column in my datagridview I get coding errors from the intellisense function that I can't seem to get correct..
 
May I please have some assistance to get this code working in VB2010?
When I try to change (gridtable_column1) to the actual name of a column in my datagridview I get coding errors from the intellisense function that I can't seem to get correct..

That's because a variable with that name already exists. You don't need a variable anyway. If you want to add a column to a DataTable then just do this:
myDataTable.Columns.Add("ColumnName", GetType(String))
 
Back
Top