在动态创建的数据表中插入按钮

时间:2017-12-22 09:37:50

标签: c# asp.net gridview datatable

我需要插入一个但是“这就是我想要的按钮”。我怎么去做呢?

显然,数据类型不能是字符串,但我应该使用什么? Html控制器?

public partial class Technician : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Fill_table();
    }

    private void Fill_table()
    {
        //creates table dynamically
        DataTable dt = new DataTable();
        //Adds column's name
        dt.Columns.Add("Ordem", typeof(string));
        dt.Columns.Add("Estados", typeof(string));
        dt.Columns.Add("Ficha Técnica", typeof(string));
        //Adds Rows
        dt.Rows.Add("Ordem X",THIS IS WHERE I WANT THE BUTTON, "Ficha Técnica");
        WorkList.DataSource = dt;
        WorkList.DataBind();
    }
}

1 个答案:

答案 0 :(得分:0)

添加如下行:

dt.Rows.Add("Ordem X", "", "Ficha Técnica");

在RowDataBound事件中:

protected void WorkList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // CHECK IF ROW IS NOT IN EDIT MODE
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // CREATE A Button
        Button btn = new Button();
        btn.ID = "btnEstados";
        btn.Text = "Estados";

        // ADD BUTTON TO EACH ROW IN 2ND COLUMN
        e.Row.Cells[1].Controls.Add(btn);
    }
}

注意:请勿忘记将属性OnRowDataBound="WorkList_RowDataBound"添加到GridView。