Thursday, August 14, 2014

Get datatable from Gridview

 protected void btnGetDatafromGrid_Click(object sender, EventArgs e)
        {
            DataTable dtEmployees = new DataTable();
            dtEmployees.Columns.Add(new DataColumn { ColumnName = "Eno", DataType = typeof(Int32) });
            dtEmployees.Columns.Add(new DataColumn { ColumnName = "Ename", DataType = typeof(string) });
            foreach (GridViewRow grdRow in GridView1.Rows)
            { 
                DataRow drEmployeeRow = dtEmployees.NewRow();
                drEmployeeRow[0] = grdRow.Cells[1].Text;
                drEmployeeRow[1] = grdRow.Cells[2].Text;
                dtEmployees.Rows.Add(drEmployeeRow);
            }
            //Now you have fully loaded datatable from Grid
            if (dtEmployees.Rows.Count > 0)
            {
                GridView1.DataSource = dtEmployees;
                GridView1.DataBind();
            }
        }

No comments:

Post a Comment