如何在使用c#在gridview中选中复选框时更新数据库值 [英] how to update the database value when the checkbox is checked iin gridview using c#

查看:136
本文介绍了如何在使用c#在gridview中选中复选框时更新数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,任何人都可以帮助我在使用c检查网格视图中的复选框时如何更新数据库值#





谢谢你提前

hi can anyone help me how to update the database value when the check box in grid view is checked using c#


thank you in advance

推荐答案

试试这个,它对我有用,应该适合你。





protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)

{

GridViewRow row = GridView1.Rows [e.RowIndex];

string constr = ConfigurationManager.ConnectionStrings [connstr]。ToString();

SqlConnection con = new SqlConnection(constr);



string id =(((Label)row.FindControl(lblid))。Text).ToString();



string comments =((( TextBox)row.FindControl(txtcomments))。Text).ToString();

// string grant =(((CheckBox)row.FindControl(CheckBox1))。选中)。 ToString();



if(((CheckBox )row.FindControl(CheckBox1))。选中== true)

{

con.Open();

SqlCommand CmdSql =新的SqlCommand(更新假设置admin_comments =''+评论+'',授予=''+真实+''其中id =+ id +,con);

CmdSql.ExecuteNonQuery();

GridView1.EditIndex = -1;

con.Close();

gridpop();

}



其他

{

con.Open();

SqlCommand CmdSql = new SqlCommand(Update leave set admin_comments =''+ comments +'',grant =''+ false +''where id =+ id +,con) ;

CmdSql.ExecuteNonQuery();

GridView1.EditIndex = -1;

con.Clo se();

gridpop();



}

}
Try this, it works for me, should work for u.


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string constr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection con = new SqlConnection(constr);

string id = (((Label)row.FindControl("lblid")).Text).ToString();

string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString();
// string grant = (((CheckBox)row.FindControl("CheckBox1")).Checked).ToString();

if (((CheckBox)row.FindControl("CheckBox1")).Checked == true)
{
con.Open();
SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments=''" + comments + "'', granted=''" + true + "'' where id=" + id + " ", con);
CmdSql.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
gridpop();
}

else
{
con.Open();
SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments=''" + comments + "'', granted=''" + false + "'' where id=" + id + " ", con);
CmdSql.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
gridpop();

}
}


这篇关于如何在使用c#在gridview中选中复选框时更新数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆