如何在C#中动态添加按钮列到表 [英] How to dynamically add a button column to table in C#

查看:93
本文介绍了如何在C#中动态添加按钮列到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我编写了下面的代码,用c#中的数组动态填充表格。我想添加最后一列,每列末尾都有按钮。点击按钮,它应删除该行。



请帮忙。



Hi,

I have written the code below that populates a table dynamically from an array in c#. I want to add the last column which will have buttons at the end of each row. On the button click, it should delete that row.

Please help.

string[] ValuesT = new string[] { code, name, age};                               
TableRow tRowT = new TableRow();
Table1.Rows.Add(tRowT);
for (int T = 0; T < 3; T++)
{
TableCell tCellT = new TableCell();
tCellT.Text = "" + ValuesT[T] + "";
tRowT.Cells.Add(tCellT); 
}





我的尝试:



我添加了这个代码来创建按钮,但它只是用按钮替换前四个单元格。



What I have tried:

I added this code to create the buttons but all it does is to replace the first four cells with buttons.

foreach (TableRow row in Table1.Rows)
{
foreach (TableCell cell in row.Cells)
{
Button btn = new Button();
btn.Text = "Delete";
btn.Click += new EventHandler(BtnDelete_Click);
cell.Controls.Add(btn);
}
}

推荐答案

foreach (TableRow row in Table1.Rows)
{
    TableCell btnCell = new TableCell();

    Button btn = new Button();
    btn.Text = "Delete";
    btn.Click += new EventHandler(BtnDelete_Click);
    btnCell.Controls.Add(btn);

    row.Cells.Add(btnCell);
}


这篇关于如何在C#中动态添加按钮列到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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