45 pts.
 Gridview data in ASP.NET data table
how to get data in DataTable from gridview contain template column as textbox?the Datatable should contain the value enter in template column

 

I used

DataTable dtData=((DataView)GridView1.DataSource).Table;

 

but given error as object reference not set for textbox in grid



Software/Hardware used:
C# asp.net
ASKED: December 11, 2010  11:09 AM
UPDATED: February 18, 2011  11:20 AM

Answer Wiki:
<b> First you take Data From the Grid View update Event by using following technique I have put here code for that ......... <pre> public void UpdateRow(object se, GridViewUpdateEventArgs e) { int id = Int32.Parse(GridView.DataKeys[e.RowIndex].Value.ToString()); GridViewRow row; row = GridView.Rows[e.RowIndex]; TextBox eNo= (TextBox)row.FindControl("txtNo"); TextBox eName = (TextBox)row.FindControl("txtName"); TextBox eDno = (TextBox)row.FindControl("txtDno"); GridView.EditIndex = -1; BindGrid(); // DataTable tabl = new DataTable(); // Create Class and initialize there field from your Grid View what ever feild you have updated // I Have Create Class here AddDataToTable AddDataToTable obj = new AddDataToTable(); obj.E_No = int.Parse(eNo.Text.ToString()); obj.E_Name = eName.Text; obj.E_Dept=int.Parse(eDno.Text); tabl= obj.ReturnTable(obj); // What I have done here is that the Data Table I have create in Class AddDataToTable bind to other Grid View GridView2.DataSource = tabl; GridView2.DataBind(); } </b> Call a Method of the Class AddDataToTable and accept that table in tabl object tabl= obj.ReturnTable(obj); In The Class I have Created a DataTable and Adding the row Which Contian the row you have update Look at the Class </pre> public class AddDataToTable { public AddDataToTable() { // // TODO: Add constructor logic here // } public DataTable RturnTable(AddDataToTable tab) { DataTable Dtable = new DataTable(); Dtable.Columns.Add("EmployeeNo", typeof(int)); Dtable.Columns.Add("EmployeeName", typeof(string)); Dtable.Columns.Add("EmployeeDpno", typeof(int)); Dtable.Rows.Add(tab.E_No,tab.E_Name, tab.E_Dept); return Dtable; } int empNumber = 0; string empName = string.Empty; int empDept = 0; public int E_No { get { return empNumber; } set { empNumber = value; } } public string E_Name { get { return empName; } set { empName = value; } } public int E_Dept { get { return empDept; } set { empDept = value; } } } I have taken only one row at a time from the grid view ............
Last Wiki Answer Submitted:  February 18, 2011  11:20 am  by  MEHRA   145 pts.
All Answer Wiki Contributors:  MEHRA   145 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _