Manipulate unsorted data in a datagridview RRS feed

  • Question

  • Hi

    I am new to .NET and I'm climbing a rather steep hill just now. :)

    Writing one of my first Winform apps I need to read some data from a file, edit it, sort it and write it back to the same file.

    Let's for arguments sake say it's data consists <firstName>, <lastName>, <position> so I thought of having data in an XML file. Read it into a dataset. Bind it to a dataGridView and then edit data. More importantly, I need to be able to move entire rows up and own. There is no data that we can use traditional sorting mechanism.

    I got as far as declaring a DataRow, deleting and inserting the row elsewhere. This works fine, except when I write to XML (Dataset.WriteXML(path)) , the order hasn't changed. I need the order to be as seen on the screen.

    Is there a way for me to ensure the order in which elements are written to XML are the same as in DataGridView?

    Is my approach using XML, dataset and DataGridView a good one? Or is there a better, easier way? (I am new)

    (c# and VS2008)
    Wednesday, February 17, 2010 10:34 PM


  • Hi BobbyWilson,

    You are right to use DataSet’s read/write xml method. If you want to keep the view of DataGridView, you can code the logic to save DataGridView data to the DataTable. The following example shows you how to populate the DataGridView from XML file and write all data to the XML keep the view of DataGridView.

    public partial class Form1 : Form
            private DataTable dt = new DataTable("table1");
            private DataSet ds = new DataSet("dataSet1");

            public Form1()
                PopulateDataGridView (dataGridView1, ds, "table1");

            private void PopulateDataGridView (DataGridView dataGridView, DataSet dsSource, string tableName)
                DataTable dt = dsSource.Tables[tableName];
                foreach (DataColumn dc in dt.Columns)
                    dataGridView.Columns.Add(dc.ColumnName, dc.ColumnName);

                foreach (DataRow dr in dt.Rows)
                    DataGridViewRow dgvRow = new DataGridViewRow();
                    for (int i = 0; i < dr.ItemArray.Length; i++)
                        DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
                        textCell.Value = dr[i];

            private DataSet DataGridViewToDataSet (DataGridView dataGridView, string tableName)
                DataSet ds = new DataSet();
                DataTable dt = new DataTable(tableName);
                foreach (DataGridViewColumn dgvColumn in dataGridView.Columns)

                foreach (DataGridViewRow dgvRow in dataGridView.Rows)
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < dgvRow.Cells.Count; i++)
                        dr[i] = dgvRow.Cells[i].Value;
                return ds;

            private void btnSave_Click(object sender, EventArgs e)
                DataSet ds = DataGridViewToDataSet(dataGridView1, "table1");

    In the example, the PopulateDataGridView method can create the DataGridView’s data according to DataTable in DataSet. On the contrary, DataGridViewToDataSet can save the data to the DataSet keeping the view of DataGridView.

    Hope it helps you. Please feel free to tell me if you have any feedback about the solution.

    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by BobbyWilson Thursday, February 18, 2010 12:14 PM
    Thursday, February 18, 2010 6:26 AM