Hi,
In gridview (2008) I want to edit all rows at a time instead of go by single row.
Can any one help me with coding example.
Thanks in advance.
Regards,
Revathi.M
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 28, 2011, 9:43 PM EST
Try this ...
Run the attachments.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void btnUpdate_Click(object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder(string.Empty);
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
string strID = GridView1.Rows[i].Cells[1].Text;
string strName = ((TextBox)
GridView1.Rows[i].FindControl("txtName")).Text;
string strLocation = ((TextBox)
GridView1.Rows[i].FindControl("txtLocation")).Text;
string strUpdate =
"Update Details set Name = '" + strName + "',Location = '" + strLocation + "' WHERE ID ='" + strID + "'";
strSql.Append(strUpdate);
}
}
}
try
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql.ToString();
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Updation";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
UncheckAll();
}
private void UncheckAll()
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkUncheck = (CheckBox)
row.FindControl("chkSelect");
TextBox txtname = (TextBox)
row.FindControl("txtName");
TextBox txtlocation = (TextBox)
row.FindControl("txtLocation");
chkUncheck.Checked = false;
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl
("txtName");
TextBox txtlocation = (TextBox)grdRow.FindControl
("txtLocation");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
else
{
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
}
Thanks
Mayur GujrathiRank #666 · 3.6K reputationPosted Nov 28, 2011, 9:51 PM EST
Revathi Murugesan31 reputationPosted Nov 29, 2011, 1:50 AM EST
Thanks for the reply.
In your code on click of check box able to edit a checked row right.
Wat i'm expecting is I want to edit a entire grid data and do one time submit.
Values are there in grid it should fetch on pageload itself. But it should be editable.
If u knows please help me.
Regards,
Revathi.M
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 29, 2011, 1:58 AM EST
Have you run the attachments.
Here in pageload event all the values are shown in the gridview.But there is CheckBox,so when you need all the data to be updated,you have to select all the CheckBoxs and enter the new values and when you click update button all are updated.
Thanks
Revathi Murugesan31 reputationPosted Nov 29, 2011, 6:45 PM EST
While clicking the checkbox its throwing error.
(NullReferencesError i'm getting)
We have to select all check box??
Check box is availbale in each row. In gridview header is not having checkbox.
help me.
Regards,
Revathi.M
Revathi Murugesan31 reputationPosted Nov 29, 2011, 7:53 PM EST
Thanks for the reply. In that attached example add row good be fine. In that I want to populate existing value in grid and all rows are editable. In my grid contain combo box value also.
Could you please help me with example.
Thanks in advance
Regards,
Revathi.M
Mayur GujrathiRank #666 · 3.6K reputationPosted Nov 29, 2011, 8:38 PM EST
And for editable dropdown refer this
http://www.codeproject.com/KB/webforms/Editable-DropDown.aspx