Pass the contents of a DataGridView to a DataSet with an already defined DataTable

1

I have a DataGridView that I feed from about TextBox and I need to pass the content from DataGrid to DataSet with a DataTable previously created, I tried to cast it like this:

DataSet ds = (DataSet)dataGridView.DataSource;

But it does not work, I would like to know if there will be any way to do it.

    
asked by Ramón Zoquier 10.06.2017 в 01:19
source

1 answer

1

Very good,

If the object DataSet already has a created object DataTable , it would be enough to link said DataTable with the DataGridView .

DataSet ds = new DataSet();

        // Referenciamos el primer objeto DataTable
        // existente en el objeto DataSet
        //
        DataTable dt = ds.Tables[0];

        // Enlazamos el control DataGridView con el
        // objeto DataTable
        //
        dataGridView1.DataSource = dt;

If we do this, as you write data in the DataGridView control, they will automatically be updated in the DataTable object in the DataSet object.

I hope it serves you.

    
answered by 11.06.2017 / 13:29
source