just want to ask if how can i update one column in gridview that was selected by the user using the checkbox,
actually there is no problem in getting the value that was checked, the only problem is that how can i update it automatically after button clicks, so far here is my code please see below.
here is my code behind please see below;
protected void btnClick_Click(object sender, EventArgs e)
{
this.ChangeStatus();
}
private void ChangeStatus()
{
//Create stringbuilder to store multiple DML statements
StringBuilder strSql = new StringBuilder(string.Empty);
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
//Create sql connection and command
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
//Loop through gridview rows to find checkbox
//and check whether it is checked or not
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("recordSelector");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
// Get the values of textboxes using findControl
string strID = GridView1.Rows[i].Cells[1].Text;
//CODE FOR UPDATE HERE ????????????????? PLEASE ADVISE
}
}
}
try
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql.ToString();
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Updating";
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("recordSelector");
chkUncheck.Checked = false;
}
}
the column that i want to update is "Status" from "P" to "A".
iam_migs
Member
262 Points
215 Posts
How to Update one databound column in gridview automatically after button clicks
Nov 10, 2011 04:50 AM|LINK
Hi to all Good day,
just want to ask if how can i update one column in gridview that was selected by the user using the checkbox,
actually there is no problem in getting the value that was checked, the only problem is that how can i update it automatically after button clicks, so far here is my code please see below.
here is my code behind please see below;
protected void btnClick_Click(object sender, EventArgs e) { this.ChangeStatus(); } private void ChangeStatus() { //Create stringbuilder to store multiple DML statements StringBuilder strSql = new StringBuilder(string.Empty); String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; //Create sql connection and command SqlConnection con = new SqlConnection(strConnString); SqlCommand cmd = new SqlCommand(); //Loop through gridview rows to find checkbox //and check whether it is checked or not for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chkUpdate = (CheckBox) GridView1.Rows[i].Cells[0].FindControl("recordSelector"); if (chkUpdate != null) { if (chkUpdate.Checked) { // Get the values of textboxes using findControl string strID = GridView1.Rows[i].Cells[1].Text; //CODE FOR UPDATE HERE ????????????????? PLEASE ADVISE } } } try { cmd.CommandType = CommandType.Text; cmd.CommandText = strSql.ToString(); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); } catch (SqlException ex) { string errorMsg = "Error in Updating"; 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("recordSelector"); chkUncheck.Checked = false; } }pkumar7
Member
105 Points
24 Posts
Re: How to Update one databound column in gridview automatically after button clicks
Jan 15, 2013 08:00 PM|LINK
You have to rebind the gridview to with the updated datasource.