Hi,
how to dynamically create bound fields in gridview and how to update and add new row to such gridview...
Plzz help
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Vijay YadavRank #1,637 · 943 reputationPosted Aug 23, 2011, 9:54 PM EDT
http://www.codeproject.com/KB/aspnet/How_to_create_custom_boun.aspx
B M SuchitraRank #2,922 · 492 reputationPosted Aug 23, 2011, 10:59 PM EDT
I have created dynamic bound fields and also have done with the updating .... Can someone tell me how can we add rows to such gridview
Vijay YadavRank #1,637 · 943 reputationPosted Aug 23, 2011, 11:15 PM EDT
For example:
public void AddNewRow()
{
DataTable dt = new DataTable();
dt.Columns.Add("SrNo", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("City", typeof(string));
foreach (GridViewRow grv in GridView1.Rows)
{
DataRow dtrow;
dtrow = dt.NewRow();
dtrow["SrNo"] = ((Label)grv.FindControl("lblSrNo")).Text;
dtrow["Name"] = ((TextBox)grv.FindControl("txtName")).Text;
dtrow["City"] = ((TextBox)grv.FindControl("txtCity")).Text;
dt.Rows.Add(dtrow);
}
DataRow dtrow1;
dtrow1 = dt.NewRow();
dt.Rows.Add(dtrow1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnAddNewRow_Click(object sender, EventArgs e)
{
AddNewRow();
}
B M SuchitraRank #2,922 · 492 reputationPosted Aug 24, 2011, 12:02 AM EDT
The code u have given is for gridview which has not been created dynamically... I have created the bound fields dynamically.. In that case how will u add a blank row to the gridview?
Vijay YadavRank #1,637 · 943 reputationPosted Aug 24, 2011, 2:18 AM EDT
Have you checked that?
B M SuchitraRank #2,922 · 492 reputationPosted Aug 24, 2011, 2:27 AM EDT
I have checked...I want to know 1 thing.
dtrow["Name"] = ((TextBox)grv.FindControl("txtName")).Text;
from where did u get txtname?
Vijay YadavRank #1,637 · 943 reputationPosted Aug 25, 2011, 1:29 AM EDT
B M SuchitraRank #2,922 · 492 reputationPosted Aug 25, 2011, 1:40 AM EDT
As i told u earlier i am adding bound fields dynamically
onpageindexchanging="GVDetails_PageIndexChanging"
onrowcancelingedit="GVDetails_RowCancelingEdit"
onrowdeleting="GVDetails_RowDeleting" onrowediting="GVDetails_RowEditing"
onrowupdating="GVDetails_RowUpdating" Font-Names="Tahoma"
Font-Size="8.25pt">
<%--
Suthish NairRank #92 · 22.8K reputationPosted Aug 25, 2011, 8:22 AM EDT