Hi...
See for all rows edit in gridview at a time.
In aspx:
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID = "chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" /></HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("UserName") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID = "lblCountry" runat="server" Text='<%# Eval("Password") %>'></asp:Label>
<asp:DropDownList ID="ddlCountries" runat="server" Visible = "false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdetails();
}
}
public void getdetails()
{
string cs = "data source=localhost;database=Murali;uid=root;password=vishwas7890;";
MySqlConnection con = new MySqlConnection(cs);
MySqlCommand cmd = new MySqlCommand("select * from registration", con);
con.Open();
cmd.ExecuteNonQuery();
MySqlDataAdapter da=new MySqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
con.Close();
gvCustomers.DataSource = ds.Tables[0];
gvCustomers.DataBind();
}
<pre lang="xml">protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (gvCustomers.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible) { isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
}
Thank u.