ASP.NET ->将 ButtonColumn 添加到 DataTable 或 DataGridView(以编程方式)

本文关键字:DataGridView 编程 方式 DataTable NET 添加 ButtonColumn ASP | 更新日期: 2023-09-27 17:56:26

弄清楚如何将ButtonColumn添加到DataTable(或者可以说是DataGrid)时遇到了一个奇怪的问题。我想做的只是能够使用数据表中的数据来告诉按钮在点击时做某事,而我似乎失败了。

谷歌搜索没有显示任何立即有用的东西,因为它们都使用ItemTemplates。

//dt.Columns.Add("Ajax Link", typeof(Literal));
ButtonColumn newButtonColumn = new ButtonColumn();
newButtonColumn.HeaderText = "Asp.Net Link";
dt.Columns.Add(); // Doesn't want newButtonColumn.
for (int i = 0; i < dt.Rows.Count; i++)
{
    /*
    Literal newAjaxLink = new Literal();
    newAjaxLink.Text = "Test";//"<button type='"button'" onclick='"AjaxButton_onClick(" + dt.Rows[i]["UserInfoID"].ToString() + "); StoreUserInfoID(" + dt.Rows[i]["UserInfoID"].ToString() + "); ShowDiv();'">Ajax Link</button>";
    dt.Rows[i]["Ajax Link"] = newAjaxLink; // @todo: HTML button that triggers an AJAX call for load the proper data into the fields. Also to make the DIV visible.
    */
    Button newButton = new Button();
    newButton.ID = dt.Rows[i]["UserInfoID"].ToString(); 
    newButton.Text = "Asp.Net Link";
    newButton.Click += new EventHandler(Button_Link_Click);
    dt.Rows[i]["Asp.Net Link"] = newButton; //@todo: Just a button to open a new window with the proper ID.
}

有什么想法吗?

ASP.NET ->将 ButtonColumn 添加到 DataTable 或 DataGridView(以编程方式)

您将按钮添加到相应内容的位置?...似乎您只是为按钮提供值,但没有添加。

试试这个

DataTable dt = new DataTable("Table"); 
DataColumn col = dt.Columns.Add("ID", typeof(Int32));
col.AllowDBNull = true;