Gridview data in ASP.NET data table

45 pts.
Tags:
ASP.NET
C#
DataGridView
GridView
GridView Control
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

Answer Wiki

Thanks. We'll let you know when a new response is added.

<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 …………

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • krunalmodi
    DataTable dt1 = new DataTable();
     dt1 = (DataTable)GridView1.DataSource;

     GridView2.DataSource = dt1;
     GridView2.DataBind();

    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following