C#Dategridview动态生成控件checkbox,为啥运行时点击checkbox不能选中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#Dategridview动态生成控件checkbox,为啥运行时点击checkbox不能选中?相关的知识,希望对你有一定的参考价值。

前台代码如下:
<asp:GridView ID="testDatagrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="testDatagrid_RowDataBound" Width="294px" Height="134px">
<Columns>
<asp:CheckBoxField HeaderText="可选" />

<asp:BoundField DataField="ToolName" HeaderText="text" />
<asp:TemplateField HeaderText="数量"></asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="数据的更新" />

后台代码实现如下:
#region GridView绑定动态生成的控件
protected void testDatagrid_RowDataBound(object sender, GridViewRowEventArgs e)


if (e.Row.RowIndex > -1)


CheckBox check = new CheckBox();
check.ID = e.Row.RowIndex.ToString();
check.AutoPostBack = true;
check.CheckedChanged += new EventHandler(check_CheckedChanged);
e.Row.Cells[0].Controls.Add(check);

TextBox text = new TextBox();
text.Width = 15;
text.ID = "txt" + e.Row.RowIndex.ToString();
text.Enabled = false;
e.Row.Cells[2].Controls.Add(text);
HiddenField hidden = new HiddenField();
hidden.Value = da.Tables[0].Rows[e.Row.RowIndex][0].ToString();
hidden.ID = "Hidd" + e.Row.RowIndex.ToString();
e.Row.Cells[2].Controls.Add(hidden);



#endregion
#region 处理checkbox所触发的事件
void check_CheckedChanged(object sender, EventArgs e)

int i = Convert.ToInt32(((CheckBox)sender).ID);
TextBox box = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
box.Enabled = true;

#endregion
#region 将客户所选的数据插入数据库
protected void Button1_Click(object sender, EventArgs e)

for (int i = 0; i < this.testDatagrid.Rows.Count; i++)

CheckBox chB = this.testDatagrid.Rows[i].Cells[0].FindControl(i.ToString()) as CheckBox;
TextBox txB = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
HiddenField hid = this.testDatagrid.Rows[i].Cells[2].FindControl("Hidd" + i.ToString()) as HiddenField;
int ii = -1;
if (chB.Checked)

if (txB.Text != null)

try

//引用 DataBase.dll
ii = new ZHXKDatabase(connStr).RunSQLCommandReturnInt("insert into Relation (ToolsID,TagID,[Desc]) values (" + hid.Value + "," + 12 + "," + int.Parse(txB.Text.Trim()) + ")");

catch (Exception exc)

Response.Write(exc.Message);



if (ii == 1)

Response.Write("OK!");




#endregion
参考技术A 是不是有Datagridview的cell点击事件,看看是否覆盖了。 参考技术B 代码写错了 当然选不中咯

vb6.0动态生成复选框并获取选中的值

比如爱好有多个,所以要动态生成多个checkbox,并且能在点击某个按钮的时候能知道选中了哪些?

在窗体上放一个复选框check1,把Index的值设置为0
动态加载:

Dim i As Integer
Dim num As Integer

num = 10 'Num为你要加载的数
For i = 1 To num - 1
Load Me.Check1(i)
Check1(i).Left = Check1(0).Left + Check1(0).Width * i
Check1(i).Top = Check1(0).Top
Check1(i).Visible = True
Check1(i).Caption = "爱好" & i
Next i
参考技术A 最简单的方法用数组控件
更改为相同的控件名~index属性即是区分

以上是关于C#Dategridview动态生成控件checkbox,为啥运行时点击checkbox不能选中?的主要内容,如果未能解决你的问题,请参考以下文章

C#如何修改dategridview里头数据

求一个c#winfrom 合并datagridview的效果通过npoi导出也是和dategridview合并效果一样的excel例子

设置DataSource后DateGridView不显示的问题

C#DateGridView怎么用Filter筛选时间类型

无法从动态生成的文件上传控件中保存文件

C#winform控件动态添加和动态移除