Click here to Skip to main content
14,738,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView, which have manually added columns (meaning entered via DataGridView and not code), I want to know how I can simply save added rows from textboxes to XML files by a button, so they can save and load when the user opens the application again.

This is what my form is looking like for reference (if it helps):
General Task List - Album on Imgur[^]


What I have tried:

The code below is what I used to add the textbox/combobox properties to the table it self.

Private Sub btnAddTask_Click(sender As Object, e As EventArgs) Handles btnAddTask.Click
    ' PURPOSE: To add a New Task to the General Task List
    ' INPUTS: Task Name (as text), Task Tags (as text), Priority Level (as Integer), Due Date (as Date), Helpful Link (as text), Note Entry (as text)
    ' OUTPUTS: Present entered properties on dataGVGeneral
    'Adds the Task Properties into new row in dataGVGeneral
    Dim dgvGeneralRow As Object() = New Object() {Nothing, txtTaskName.Text, txtTaskTags.Text, cmbPriority.SelectedItem.ToString(), dateDUE.Value.ToShortDateString(), txtLink.Text, txtNoteEntry.Text}
    txtTaskName.Text = ""
    txtTaskTags.Text = ""
    txtLink.Text = ""
    txtNoteEntry.Text = ""
End Sub
Updated 21-Jul-20 16:36pm

1 solution

how I can simply save
well, I guess it depends on your definition of 'simply' doesn't it ?

My definition of 'simply' would be using a datatable and the methods it already has ...
'Setup, somewhere, maybe Init
'Create backing data-table
Dim dt As New DataTable("Src")
dt.Columns.Add("task", GetType(String))
dt.Columns.Add("tags", GetType(String))
dt.Columns.Add("priority", GetType(Integer))
' .. and so forth
'Add datatable as datagridview's data source
'Assuming datagridview's name is dgv (original, not)
dgv.DataSource = dt;

'As part of Save Event from button click or such
Dim file = "c:\somedirectory\someotherdirectory\tasks.xml"
dt.WriteXml(file, XmlWriteMode.WriteSchema)

'Maybe as part of Form Load event/Setup
'Assuming dt and file defined 

But your definition of 'Simple' may vary - the 'Setup/Init' section I've demonstrated could be combined with the 'maybe as part of Form Load event/Setup' obviously - my intent is more to make you aware of the methods the DataTable Class provides, rather than a 're-invent the wheel' approach
Shaheer Rizwan 22-Jul-20 2:03am
Thank you I'll have a look
Shaheer Rizwan 22-Jul-20 20:00pm
Is there a way to just save all the inputted data in any file and then select a row via the checkbox column and send that row to another datagridview in another form and save it?
Garth J Lancaster 22-Jul-20 23:08pm
sure - what I've got there will save all of lets say the first datagridview dgv to a file. Let's assume that dgv has the first column as a checkbox that if ticked you want to transfer that row to a second datagridview dgv2 - then roughly, build a new datatable dtt with the rows for datagrid2/dgv2, loop through the selected rows of datagrid 1, add those rows to the 'transfer datatable, then set that datatable as the source for the second datagridview dgv2, ...
DataTable dtt = new DataTable();
// ... and so on   
foreach (DataGridViewRow item in dgv.Rows)
  if ((bool)item.Cells[0].Value == true)     
    bool isSelected = Convert.ToBoolean(row.Cells["Col1"].Value);        
    if (isSelected)        
      dtt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, ... );        
dgv2.DataSource = dtt;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900