在GridView中删除多个复选框 [英] multiple checkbox delete in gridview
本文介绍了在GridView中删除多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除按钮上的删除按钮单击事件的复选框.但是我无法获取要删除的ID
相应的复选框..请帮助
代码如下.
提前谢谢.
i want to delete checkbox on delete button click event..but i cant get id to delete
appropriate checkbox..please help
code is given bellow..
thanx in advance.
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\rAvIrAj\Downloads\Ravi\Ravi\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Grid_Bind();
}
}
public void Grid_Bind()
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(); ;
DataTable dt = new DataTable();
try
{
cn.Open();
cmd.CommandText = "Select * from Student_Master";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
da.Fill(dt);
gv1.DataSource = dt;
gv1.DataBind();
cn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void chk1_CheckedChanged1(object sender, EventArgs e)
{
try
{
if (((CheckBox)gv1.HeaderRow.FindControl("chk1")).Checked==true)
{
foreach (GridViewRow row in gv1.Rows)
{
((CheckBox)row.FindControl("chk2")).Checked = true;
}
}
else
{
foreach (GridViewRow row in gv1.Rows)
{
((CheckBox)row.FindControl("chk2")).Checked = false;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btn_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv1.Rows)
{
if (((CheckBox)row.FindControl("chk2")).Checked == true)
{
int id =
SqlCommand cmd = new SqlCommand("delete from Student_Master where id="+id,cn);
Grid_Bind();
ll.Text = "successful";
}
}
}
推荐答案
尝试一下.
try this.
//num is the index of the id column ex. gv1.Rows(i).Cells(1).Text
string id = gv1.Rows(i).Cells(num).Text
您可以使用id这个:-
int id =
you can used id like this:-
int id=
int.parse(gv1.Rows(index).Cells(num).Text.ToString());
index is int type have column no index.like id,name,s_name
than asign index is 0
<pre></pre>
我希望您的问题能解决,否则请发表评论.
这篇关于在GridView中删除多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文