1

How can I fill Datatable with DataGridView data (VB/C# .NET)?

1
  • Can you elaborate your question a bit. Is your datagridview binded with another datatable? How the data is filled in datagridview? etc Commented Dec 2, 2010 at 5:16

1 Answer 1

5

Assuming that it is a Winform

below code can be of help

        //dgv is the name of your data grid view.

        DataTable dt = new DataTable();
        DataColumn[] dcs = new DataColumn[]{};

        foreach (DataGridViewColumn c in dgv.Columns)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = c.Name;
            dc.DataType = c.ValueType;
            dt.Columns.Add(dc);

        }

        foreach (DataGridViewRow r in dgv.Rows)
        {
            DataRow drow = dt.NewRow();

            foreach (DataGridViewCell cell in r.Cells)
            {
                drow[cell.OwningColumn.Name] = cell.Value;
            }

            dt.Rows.Add(drow);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.