Mostly time Developers do not use Editable grid-view because of lot’s of reason… Developers prefer manually coding but editable gridview is good way to update database.
I am using Gridview by Wizard do Update. I am submitting all process to step by step first fill then create bool function and go to assemble row editing & updating Event.
Introduction
Editable GridView - All Columns in Grid-View are Editable
I am using Gridview by Wizard do Update … Lets see this step by step
Step 1
Put Grid-view in your. aspx page then u write code on page load fill grid-view. After filling gird view..you
please set click on grid-view click EDIT Column…convert to field to Convert this field into a TemplateField
Gridview
<asp:GridView ID="gv1" runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource4" OnRowEditing="gv1_RowEditing"
OnRowUpdating="gv1_RowUpdating" CellPadding="4" ForeColor="#333333">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ssno" HeaderText="ssno" InsertVisible="False"
ReadOnly="True"
SortExpression="ssno"
/>
<asp:BoundField DataField="qacno" HeaderText="qacno" SortExpression="qacno"
InsertVisible="False"
ReadOnly="True"
/>
<asp:BoundField DataField="strd" HeaderText="strd" SortExpression="strd"
InsertVisible="False"
ReadOnly="True"
/>
<asp:TemplateField HeaderText="sno" InsertVisible="False" SortExpression="sno">
<EditItemTemplate>
<asp:TextBox ID="TextBox6"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("sno") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("sno") %>'></asp:Label>
<asp:TextBox ID="TextBox6"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("sno") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="parameters" SortExpression="parameters">
<EditItemTemplate>
<asp:TextBox ID="TextBox2"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("parameters") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("parameters") %>'></asp:Label>
<asp:TextBox ID="TextBox2"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("parameters") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="unit" SortExpression="unit">
<EditItemTemplate>
<asp:TextBox ID="TextBox3"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("unit") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("unit") %>'></asp:Label>
<asp:TextBox ID="TextBox3"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("unit") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="spval" SortExpression="spval">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("spval") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("spval") %>'></asp:Label>
<asp:TextBox ID="TextBox1"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("spval") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="torval" SortExpression="torval">
<EditItemTemplate>
<asp:TextBox ID="TextBox4"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("torval") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("torval") %>'></asp:Label>
<asp:TextBox ID="TextBox4"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("torval") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="obtval" SortExpression="obtval">
<EditItemTemplate>
<asp:TextBox ID="TextBox5"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("obtval") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server"
Visible='<%# !(bool)
IsInEditMode %>' Text='<%#
Bind("obtval") %>'></asp:Label>
<asp:TextBox ID="TextBox5"
runat="server"
Visible='<%# IsInEditMode %>' Text='<%#
Bind("obtval") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Step 2
Firstly declare
private bool
isEditMode = false;
then Create IsInEditMode bool function in .cs
page
protected bool IsInEditMode
{
get { return this.isEditMode;
}
set { this.isEditMode = value;
}
}
Step 3
After cretae bool function assemble
RowEditing event for edit mode visible true .
protected void gv1_RowEditing(object
sender, GridViewEditEventArgs e)
{
isEditMode = true;
}
Step 4
After editable grid-view mode you are assemble RowUpdating event for update data base
protected void gv1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
for (int i = 0; i <= gv1.Rows.Count - 1; i++)
{
int
sno = Convert.ToInt32(gv1.Rows[i].Cells[1].Text);
String
pram = ((TextBox)gv1.Rows[i].FindControl("Textbox2")).Text;
String
unit = ((TextBox)gv1.Rows[i].FindControl("Textbox3")).Text;
String
spval = ((TextBox)gv1.Rows[i].FindControl("Textbox1")).Text;
String
torval = ((TextBox)gv1.Rows[i].FindControl("Textbox4")).Text;
String
obtval = ((TextBox)gv1.Rows[i].FindControl("Textbox5")).Text;
string
constr1;
IFormatProvider
culture = new CultureInfo("fr-Fr", true);
constr1 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
con = new
SqlConnection(constr1);
con.Open();
string
sql = "UPDATE [cqac] SET parameters='" + pram + "', unit='" + unit + "',[spval] = '" + spval + "',torval='" + torval + "',obtval='"+ obtval +"' WHERE
[Ssno] =" + sno + "";
cmd = new
SqlCommand(sql, con);
int
res;
res = cmd.ExecuteNonQuery();
res = 1;
con.Close();
}
}
Conclusion
In ASP.Net Control Girdview gives good support by dynamic data
and Update by Wizard facility. In Grid-View editable view u do many records updated by one click..
Thanks & Regrad’s
Prabhakar