1)
What is issue
Asp.net gridview provides the functionality of editing but at a time only single row you have to edit a row first then update it will make a round to server rebind whole grid and then you can edit next row.
Today i was thinking if it is scenario that all the rows should be in edit mode on load time and user can edit all the rows and update them at a time.i have a littil R&D on this topic and found two solution one is explained here and second will be next time inshalla.the second way is two make a customize contorl which inherits from systm.web.ui.webcontrol.gridview.and override some functions as per required.
so here is the first one
2)
Explanation
following steps are involved in this tutorial.
1 make a new website with one default.aspx page.
2 drop a gridview toolbox.
3 drop a button.
4 drop a sqldatasource with enable of insert,update,delete.
i have a table "Employes" in my database with columns ('P_ID','Name','Age','City','Departement')
3)
Demonstration
In columns add one asp:boundfield, and four asp:templatefield.
In bound field P_ID will be displayed it will be readonly and datakey of gridview.
In templatefield add itemtemplate and in itemtemplate add textbox controls.so the grid will be shown in edit mode on load time.
on rowdatabound event of gridview store the values of orignal table in a datatable.
write a method which will check the row is edited or not.
on the click event of update button check each row if edited then update that row.
here is code with further explanation.
4)
Explaination
now do follow the steps in explanation step.
1 make a demo website.
2)Add a gridview controle with following code.
<asp:GridView ID="grdEmployes" runat="server" DataSourceID="grdEmployeDS" OnRowDataBound="grdNames_RowDataBound1"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="P_ID">
<Columns>
<asp:BoundField DataField="P_ID" HeaderText="P_ID" InsertVisible="False" ReadOnly="True"
SortExpression="P_ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%#Bind("Age") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="City">
<ItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%#Bind ("City") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Departement">
<ItemTemplate>
<asp:TextBox ID="txtDepartment" runat="server" Text='<%#Bind("Departement") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on row databound event of grid make a copy of non edited datatable.here is code in .cs file
protected void grdNames_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (tableCopied == false)
{
orignalTable = ((DataRowView)e.Row.DataItem).Row.Table;
ViewState["orignalData"] = orignalTable;
tableCopied = true;
}
}
}
3)add a update button
In aspx file:
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
In codebehind file :
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdEmployes.Rows)
{
if (checkRow(row) == true)
{
grdEmployes.UpdateRow(row.RowIndex, false);
}
}
}
here in if statement checkrow(row) is the mehtod which will check either row is edited or not.so i will explain it here first before further step.
private bool checkRow(GridViewRow row)
{
int pid;
string Name;
string Age;
string City;
string Departement;
DataTable table = (DataTable)ViewState["orignalData"];
pid = Convert.ToInt32(grdNames.DataKeys[row.RowIndex].Value.ToString());
TextBox txtName = (TextBox)row.FindControl("txtName");
TextBox txtAge = (TextBox)row.FindControl("txtAge");
TextBox txtCity = (TextBox)row.FindControl("txtCity");
TextBox txtDepart = (TextBox)row.FindControl("txtDepartment");
Name = txtName.Text;
Age = txtAge.Text;
City = txtCity.Text;
Departement = txtDepart.Text;
DataRow dr = table.Select(string.Format("P_ID={0}", pid))[0];
if(!Name.Equals(dr["Name"].ToString()))return true;
if(!Age.Equals(dr["Age"].ToString())) return true;
if(!City.Equals(dr["City"].ToString())) return true;
if(!Departement.Equals(dr["Departement"].ToString())) return true;
return false;
}
Normal Functionality here:
Bulk editing enabled here:
I will appreciat if someone enhance this functionality,and feel sorry if it is to feel confusing.
Thanks
Muhammad Khalil