考虑以下代码
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dtCol = new DataColumn();
dtCol = new DataColumn("Name", typeof(string));
dt.Columns.Add(dtCol);
dtCol = new DataColumn("Address", typeof(string));
dt.Columns.Add(dtCol);
dtCol = new DataColumn("Account No", typeof(ButtonField));
dt.Columns.Add(dtCol);
ButtonField bfname = new ButtonField {
HeaderText = "Account No",
DataTextField="Account No",
Text = "Klik",
ButtonType = ButtonType.Link,
CommandName = "ExecuteMe" };
string[] strDataRow = new string[3];
for (int intX = 0; intX < 10; intX++)
{
strDataRow[0] = "Name" + Convert.ToString(intX);
strDataRow[1] = "Address" + Convert.ToString(intX);
strDataRow[2] = bfname.Text; //Error Type of value has a mismatch with column typeCouldn't store <Account No0> in Account No Column. Expected type is ButtonField.
ButtonField btnField = bfname; // No Error but not appear in GridView1
dt.Rows.Add(new object[] {strDataRow[0], strDataRow[1], strDataRow[2]});
//dt.Rows.Add(new object[] { strDataRow[0], strDataRow[1], btnField }); // No Error but not appear in GridView1
/* Error Message on strDataRow[2]
Type of value has a mismatch with column typeCouldn't store <Account No0> in Account No Column. Expected type is ButtonField.
*/
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
如何将ButtonField存储在列设置为ButtonField的数据流中?ButtonField。文本出现在每个DataRow
和
是否可以将字符串值存储在为ButtonField设置的列的数据row中。当字符串是click时,然后是ButtonField。CommandName将执行。
注*:在ASPX页面中的GirdView代码如下。
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
我不确定你的问题,但不是把按钮在数据表,我认为是不可能的,为什么不创建一个GridView和添加一个TemplateField包含您的按钮