Click here to Skip to main content
15,887,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, I want to enter the values in database through gridview. Can I take data from database to a gridview and add a textbox to each of the row so that I can enter the value for each row in corresponding textboxes and update database.Please anybody can help me....
Posted

 
Share this answer
 
yes you can. add a new control on rowdatabound of the griview.

in .aspx page add in your gridview control
C#
OnRowDataBound="gvGridView_RowDataBound"


like this..

C#
<asp:GridView ID="gvGridView" runat="server" AutoGenerateColumns="False" OnRowCommand="gvGridView_RowCommand" OnRowDataBound="gvGridView_RowDataBound">



and in code behind(on aspx.cs page)
C#
protected void gvGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
    {
                TextBox tx = new TextBox();  //here i m adding a control.
                tx.ID = "txtCheck";
                tx.Attributes.Add("runat", "server");
                int i = e.Row.Cells.Count;
                i = i - 1;
                e.Row.Cells[i].Controls.Add(tx);   //textbox is added as last column of grid
}
}
 
Share this answer
 
v2
Comments
Zukiari 5-Dec-11 7:06am    
I'm new to this asp.net.I don't know how to use rowdatabound control. Can you please tell me more abut that.
S.P.Tiwari 6-Dec-11 0:22am    
try with this updated solution. if its solve your problem plz rate 5
Codes DeCodes 2-Oct-13 2:02am    
how to add textbox at the second row?? and not at the last column??
S.P.Tiwari 3-Oct-13 2:49am    
do you mean the gridview second row ?
then chek if the e.rowIndex == 1
and for column you have to set the index
e.Row.Cells[3].Controls.Add(new Textbox);
Codes DeCodes 2-Oct-13 2:02am    
how to add textbox at the second row?? and not at the last column??
You can refer this link:
ADDING NEW ROW TO THE GRID VIEW[^]
 
Share this answer
 
You can do like this :
ASP.NET
<asp:gridview id="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" xmlns:asp="#unknown">
 <columns>
  <asp:templatefield headertext="TextBox">
   <itemtemplate>
    <asp:textbox id="TextBox1" runat="server"></asp:textbox>
   </itemtemplate>
  </asp:templatefield>
   <itemtemplate>
    <asp:textbox id="TextBox2" runat="server"></asp:textbox>
   </itemtemplate>
  
 </columns>
</asp:gridview>
 
Share this answer
 
Comments
Anil Lenka 17-Jun-15 7:43am    
Where is your .cs file ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900