Make all Row Editable of a GridView

To Make All Row Editable of a GridView follow these steps :

Steps:


1. Create one GridView with all field as Template Field and add TextBox for Item Template . So, when it displays the data it will display in Textbox so that you can edit those field without click edit button.
2. Create One Button. Set Text as Update. So that when we click on this button, this will update the database as per the changed data in gridview.

Look at the following code to do so...

CODE:



< asp:GridView ID="GridView1" runat="server" Style="z-index: 106; left: 93px; position: absolute;
top: 283px" Width="575px" EditIndex="1" AutoGenerateColumns="False" >
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID="txt1" runat="server" MaxLength="3" Style="position: static" CssClass ="gv" text = '<%# Eval("slNo") %>' AutoPostBack ="false" TabIndex ="0" Width="25px" Height ="15px" Visible ="true" >< / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID="txt2" runat="server" MaxLength="3" Style="position: static" CssClass ="gv" text = '<%# Eval("Name") %>' AutoPostBack ="false" TabIndex ="0" Width="250px" Height ="15px" Visible ="true" >< / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID="txt3" runat="server" MaxLength="3" Style="position: static" CssClass ="gv" text = '<%# Eval("Address") %>' AutoPostBack ="false" TabIndex ="0" Width="150px" Height ="15px" Visible ="true" >< / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns>
< / asp:GridView >


Then Place the following code in the button click event,

CODE:



protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i
{
GridViewRow gvr = GridView1.Rows[i];
string str1 = ((TextBox)gvr.FindControl("txt1")).Text;
string str2 = ((TextBox)gvr.FindControl("txt2")).Text;
string str3 = ((TextBox)gvr.FindControl("txt3")).Text;
//code for saving the data into database
}

}


Regards,
Manoranjan Sahoo
Visit My Blog: http://msahoo.wordpress.com


Comments

Author: Abhinav Dawra07 Jun 2009 Member Level: Gold   Points : 0

Seems to be a good code.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: