save edited DataGridView to Datatable

LordTitiKaka

I'm new to DataGridView I want to create simple application that read XML to dataGridview Edit it than save it back to XML My Problem is at the saving to DataSet/DataTable this is my code :

    //I want to change column name than save it to file
    var column = DG_dataGridView.Columns[ColumnIndex];

    column.Name = ColumnName;
    column.HeaderText = ColumnName;

    string dataFile = "c:\test.xml"
    DataTable dataTable = (DataTable)DG_dataGridView.DataSource;

    dataTable.DataSet.WriteXml(dataFile);

when I view the dataTable object I see that none of my changes were set although I see the changes in GUI and dataGridView object

Shaharyar

Remember you're making changes in GridView not in DataSource. You will have to traverse the GridView and generate a new DataTable.

Create a separate function:

private void WriteToFile()
{
    DataTable dataTable = new DataTable();

    //create columns
    for (int i = 0; i < DG_dataGridView.Columns.Count; i++)
    {
        dataTable.Columns.Add("column"+i.ToString());
    }

    //populate data
    foreach (GridViewRow row in DG_dataGridView.Rows)
    {
        DataRow dr = dataTable.NewRow();
        for(int j = 0; j < DG_dataGridView.Columns.Count; j++)
        {
            dr["column" + j.ToString()] = row.Cells[j].Text;
        }

        dataTable.Rows.Add(dr);
    }

    //write to file
    string dataFile = "c:\test.xml"
    dataTable.DataSet.WriteXml(dataFile);
}

and use it:

//I want to change column name than save it to file
var column = DG_dataGridView.Columns[ColumnIndex];

column.Name = ColumnName;
column.HeaderText = ColumnName;

//write to file
WriteToFile();

Code reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I want to save any changes to a DataTable in a DataGridView

From Dev

DataGridView filter hiding edited items

From Dev

Change of the edited cell in a datagridview cell

From Dev

How to save edited content in html

From Dev

Save an edited file in nano, but no permissions

From Dev

Save edited data in html rows

From Dev

Angular: how to save edited values?

From Dev

DataGridView binded to DataTable with ComboBox

From Dev

DataGridView selected rows to DataTable

From Dev

DatagridView not loading from DataTable

From Dev

DataGridView DataError on Bound DataTable

From Dev

DataGridView binded to DataTable with ComboBox

From Dev

DatagridView not loading from DataTable

From Dev

Access updated DataTable edited with CellEdit in R shiny

From Dev

Save datagridview to mysql

From Dev

save changes of DataGridView to database

From Dev

Save datagridView into sql database

From Dev

Save datagridview data to xml

From Dev

Django. How to save a ContentFile edited with Pillow

From Dev

How do I save files edited with vim?

From Dev

How do I save files edited with vim?

From Dev

Save edited cell/s from JTable to database

From Dev

C#: Programmatically save an edited excel workbook

From Dev

Datagridview linked to datatable not getting updated

From Dev

C# DataGridView is not updating with DataTable

From Dev

DataTable/DataGridView Row Number Display

From Dev

DataTable to Pre Defined DataGridView Columns

From Dev

Read .csv to datatable and fill a datagridview

From Dev

CancelEdit does not keep focus on edited cell in DataGridView c#