How to store ButtonField in a datarow where the column is set for ButtonField

落爺英雄遲暮 提交于 2019-12-12 03:47:19

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/15839399/how-to-store-buttonfield-in-a-datarow-where-the-column-is-set-for-buttonfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!