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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mayur GujrathiPosted Nov 30, 2011, 1:38 AM
And for editable dropdown refer this
http://www.codeproject.com/KB/webforms/Editable-DropDown.aspx
Revathi MurugesanPosted Nov 30, 2011, 12:53 AM
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
Revathi MurugesanPosted Nov 29, 2011, 11:45 PM
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
Satyapriya NayakPosted Nov 29, 2011, 6:58 AM
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 MurugesanPosted Nov 29, 2011, 6:50 AM
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
Mayur GujrathiPosted Nov 29, 2011, 2:51 AM
Satyapriya NayakPosted Nov 29, 2011, 2:43 AM
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