gridview中checkbox不可选

Qu-俊 2012-05-16 08:42:06
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.Label1.Text = "";
CheckBox ck = (CheckBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0];

bool b = ck.Checked;
if (!b)
{
string uname = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
string date = this.GridView1.Rows[e.RowIndex].Cells[3].Text.ToString();
string num = this.GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();

if (bs.doProcedure("exec prc_addcoins '" + uname + "','" + date + "'," + num))
{
this.Label1.Text = "<script>alert('处理成功')</script>";
this.fillTable();
}
}
else
{
this.Label1.Text = "<script>alert('已经处理')</script>";


本人想在gridview中增加checkbox选中后更新数据功能,但调试后checkbox不可选,求各位大虾帮忙求解。
...全文
227 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
HopeInHeart 2012-05-16
  • 打赏
  • 举报
回复
除了代码之外,还有一个很细节的问题,就是当datagridview设为ReadOnly属性时候,添加的CheckBox是不可选的,所以不能把dataGridView设为只读的
孟子E章 2012-05-16
  • 打赏
  • 举报
回复
给你个例子
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" EnableViewState="true" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
if (!Page.IsPostBack)
{
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("aa", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("vv", typeof(System.Boolean)));
System.Random rd = new System.Random();
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = i.ToString();
dr[1] = i;

dt.Rows.Add(dr);
}

GridView1.DataSource = dt;
GridView1.DataBind();
}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox ck = (CheckBox)e.Row.Cells[1].Controls[0];
ck.Enabled = true;
}
}


protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow x in GridView1.Rows)
{
CheckBox ck = (CheckBox)x.Cells[1].Controls[0];
if (ck.Checked)
{
Response.Write("<li>删除:aa=" + x.Cells[0].Text);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除所选的行" />
</form>
</body>
</html>

孟子E章 2012-05-16
  • 打赏
  • 举报
回复
如果你不想改模板列,你可以这样
CheckBox ck = (CheckBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
ck.Enabled=true;
孟子E章 2012-05-16
  • 打赏
  • 举报
回复
使用模板列,不要使用绑定列进行FindControl是最好的方法
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>

另外,RowDeleting无需查找CheckBox就可以实现的
IT-Style 2012-05-16
  • 打赏
  • 举报
回复

CheckBox ck = (CheckBox)this.GridView1.Rows[e.RowIndex].FindControl("controlName");


这样试一试
Qu-俊 2012-05-16
  • 打赏
  • 举报
回复
Qu-俊 2012-05-16
  • 打赏
  • 举报
回复

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧