datatable to datagridview code example

Example 1: how to import datagridview to datatable in c#

public DataTable GetDataTable()
{
    DataTable dtLocalC = new DataTable();
    dtLocalC.Columns.Add("splr_Slno");
    dtLocalC.Columns.Add("splr_Cntctnm");
    dtLocalC.Columns.Add("splr_Cntctdesig");
    dtLocalC.Columns.Add("splr_Cntctmoblno");
    dtLocalC.Columns.Add("splr_Cntctemail");

    DataRow drLocal = null;
    foreach (DataGridViewRow dr in dgvSpplrfrm.Rows)
    {
        drLocal = dtLocalC.NewRow();
        drLocal["splr_Slno"] = dr.Cells["splr_Slno"].Value;
        drLocal["splr_Cntctnm"] = dr.Cells["splr_Cntctnm"].Value;
        drLocal["splr_Cntctdesig"] = dr.Cells["splr_Cntctdesig"].Value;
        drLocal["splr_Cntctmoblno"] = dr.Cells["splr_Cntctmoblno"].Value;
        drLocal["splr_Cntctemail"] = dr.Cells["splr_Cntctemail"].Value;
        dtLocalC.Rows.Add(drLocal);
    }

    return dtLocalC;
}

Example 2: setting datagrid itemssource to datatable

DGrid.ItemsSource = dt.AsDataView();