如何将ButtonField存储在为ButtonField设置列的数据行中 [英] How to store ButtonField in a datarow where the column is set for ButtonField

查看:161
本文介绍了如何将ButtonField存储在为ButtonField设置列的数据行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码:

p $ p $ 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]; (int intX = 0; intX< 10; intX ++)
{
strDataRow [0] =Name+ Convert.ToString(intX)


;
strDataRow [1] =地址+ Convert.ToString(intX);
strDataRow [2] = bfname.Text; //错误值的类型与列类型不匹配不能存储< Account No0>在帐户没有列。预期的类型是ButtonField。
ButtonField btnField = bfname; //没有错误但没有出现在GridView1中

dt.Rows.Add(new object [] {strDataRow [0],strDataRow [1],strDataRow [2]});
//dt.Rows.Add(new object [] {strDataRow [0],strDataRow [1],btnField}); //没有错误但没有出现在GridView1中

/ * strDataRow [2]的错误消息
值的类型与列类型不匹配无法存储< Account No0>在帐户没有列。预期的类型是ButtonField。
* /

}

GridView1.DataSource = dt;
GridView1.DataBind();




$ b $ p
$ b

如何将ButtonField存储在列中的数据行中为ButtonField设置。 ButtonField.Text如何在每个DataRow中显示



是否可以将字符串值存储在数据行这是为ButtonField设置的列。当字符串被点击时,ButtonField.CommandName将被执行。



注意*:ASPX页面中的GirdView代码如下。

 < asp:GridView ID =GridView1runat =server> 
< / asp:GridView>

在此先感谢...

解决方案

我不确定你的问题,但不是把按钮放在DataTable中,我认为不可能为什么不创建一个GridView并添加一个包含你的按钮的TemplateField。 b $ b

consider the following code

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();

    }

How do I store ButtonField in a datarow where the column is set for ButtonField. How the ButtonField.Text appear in each DataRow

And

Is it possible to store string value in a datarow which is the column is set for ButtonField. When the string is click , then the ButtonField.CommandName will execute.

Note* : The GirdView code in ASPX page as follows.

<asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

Thanks in advance...

解决方案

Im not sure about your question, but instead of put the button in the DataTable, what I suposed as impossible why not create a GridView and add a TemplateField containing your button.

这篇关于如何将ButtonField存储在为ButtonField设置列的数据行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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