1

What's the equivalent in .NET to convert my DataGrid to a DataTable (DataGrid.ItemsSource isn't defined in .Net csharp.

Thanks!

DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();

EDIT

This isn't a duplicate since the previous code is for WPF and I'm looking for an asp.net mvc csharp answer.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
J. Lavoie
  • 335
  • 2
  • 5
  • 23
  • Possible duplicate of [How to convert DataGrid to dataTable](https://stackoverflow.com/questions/21827339/how-to-convert-datagrid-to-datatable) –  Jun 19 '17 at 19:04
  • @arbitrarystringofletters No. The duplicate post's code is same as the OP's code and since he/she mentioned *`DataGrid.ItemsSource` isn't defined* so he/she definitely is not looking for a WPF solution. – Salah Akbari Jun 19 '17 at 19:09

4 Answers4

2

The ItemsSource is for WPF. Use the DataSource and cast it to DataTable like this:

dt = (DataTable)DataGrid1.DataSource;

EDIT: And if you get into trouble with above approach, you can use a custom method like this:

private DataTable ToDataTable(DataGridView dataGridView)
{
    var dt = new DataTable();
    foreach (DataGridViewColumn dataGridViewColumn in dataGridView.Columns)
    {
        if (dataGridViewColumn.Visible)
        {
            dt.Columns.Add();
        }
    }
    var cell = new object[dataGridView.Columns.Count];
    foreach (DataGridViewRow dataGridViewRow in dataGridView.Rows)
    {
        for (int i = 0; i < dataGridViewRow.Cells.Count; i++)
        {
            cell[i] = dataGridViewRow.Cells[i].Value;
        }
        dt.Rows.Add(cell);
    }
    return dt;
}

And then use it:

var dataTable = ToDataTable(dataGridView1);

Also MoreLinq is a good choice in case the type of Datasource is a list. Check this solution to know how to use it: https://stackoverflow.com/a/42550827/2946329

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

If you are referring to the System.Windows.Forms.DataGrid or System.Web.UI.WebControls.DataGrid, then the best way would be to cast the Datasource property to a DataTable.

Of course the Datasource property has to actually be a DataTable underlying type to begin with. You need to know the underlying type of the object stored in the Datasource property.

If the underlying type of Datasource is a generic list, then this SO post should help: How to convert a list into data table

FYI - The Windows Forms DataGrid control, according to Microsoft, has been replaced by the DataGridView.

bnem
  • 95
  • 1
  • 7
  • I'm referring to the `System.Web.UI.WebControls.DataGrid` – J. Lavoie Jun 19 '17 at 19:24
  • @J.Lavoie yes saw your edit and updated my answer, added a link to converting list to datatable – bnem Jun 19 '17 at 19:24
  • I have an error when casting, but maybe it's because the underlying type of the object stored is a class object! – J. Lavoie Jun 19 '17 at 19:31
  • @J.Lavoie in your comment on the other answer, you had an error because you were trying convert a generic list to a `Datatable`. Use the link I provided in my answer. – bnem Jun 19 '17 at 19:36
  • Ok this work perfectly except that my datetime values are converted in decimal! – J. Lavoie Jun 19 '17 at 19:41
0

if there is visible columns in datagridview you can use

    private DataTable ToDataTable(DataGridView dataGridView)
    {
        var dt = new DataTable();
        int columnCount = 0;
        List<int> columnNumbers= new List<int>();

        foreach (DataGridViewColumn dataGridViewColumn in dataGridView.Columns)
        {


            if (dataGridViewColumn.Visible)
            {
                dt.Columns.Add(dataGridViewColumn.Name);                   
                columnNumbers.Add(columnCount);

            }
            columnCount++;
        }

        var cell = new object[columnNumbers.Count];
        foreach (DataGridViewRow dataGridViewRow in dataGridView.Rows)
        {
            int i = 0;
            foreach (int a in columnNumbers)
            {
                cell[i] = dataGridViewRow.Cells[a].Value;
                i++;
            }
            dt.Rows.Add(cell);
        }
        return dt;
    }
0

The custom method does not take into account the hidden columns. You are getting an error, because you have too many cells for the columns copied.

You can use :

int dgv1RowCount = dgv1.Rows.Count;
int numOfColumns = dgv1.Columns.GetColumnCount(DataGridViewElementStates.Visible) ;
int numCells = dgv1RowCount * numOfColumns;
// use numCells in the for loop
for (int i = 0; i < numOfCells ; i++)
{
    enter code here
}
hellow
  • 12,430
  • 7
  • 56
  • 79
Nancy
  • 1
  • 1