0

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>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
cahmad
  • 23
  • 1
  • 10
  • 1
    Welcome to StackOverflow! You may be more likely to get useful answers if you can narrow the code down and ask only one question about it. – ASGM Apr 05 '13 at 17:16

1 Answers1

0

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.

Fals
  • 6,813
  • 4
  • 23
  • 43
  • Well, I have the same opinion as yours, however since the DataTable still accept the type of ButtonField, i beleive that there must be a way to do that. Otherwise .NET Framework must correct this so that we knew that the type of ButtonField cannot accept it directly as in the code above. – cahmad Apr 05 '13 at 18:42