3

I have a problem adding a column with buttons in GridView.

As you see from the code below, the data source from teh GridView is a DataTable. I need to add an additional column to the table with a button.

From the code below, I get an error message saying:

Value of type 'System.Windows.Forms.DataGridViewButtonColumn' cannot be converted to 'System.Web.UI.WebControls.DataControlField'.

    Dim dt_AllGroupsSetUp2 As New DataTable()
    dt_AllGroupsSetUp2.Columns.Add("Name", Type.GetType("System.String"))
    dt_AllGroupsSetUp2.Columns.Add("Age", Type.GetType("System.String"))
    dt_AllGroupsSetUp2.Columns.Add("Hight", Type.GetType("System.String"))

    For i As Integer = 0 To 7
        dt_AllGroupsSetUp2.Rows.Add()
        dt_AllGroupsSetUp2.Rows(i)(0) = "John"
        dt_AllGroupsSetUp2.Rows(i)(1) = 10
        dt_AllGroupsSetUp2.Rows(i)(2) = 70
    Next

    GV_DataByGroupAct.DataSource = dt_AllGroupsSetUp2
    Dim buttonColumn As New DataGridViewButtonColumn
    buttonColumn.Name = "Button"
    GV_DataByGroupAct.Columns.Add(buttonColumn)
    GV_DataByGroupAct.DataBind()

I tried the folling also but returned the following error: 'New' cannot be used on a class that is declared 'MustInherit'.

    GV_DataByGroupAct.DataSource = dt_AllGroupsSetUp2
    Dim buttonColumn As New DataControlField
    GV_DataByGroupAct.Columns.Add(buttonColumn)
    GV_DataByGroupAct.DataBind()

Any ideas?

Thanks

Andy G
  • 19,232
  • 5
  • 47
  • 69
Selrac
  • 2,203
  • 9
  • 41
  • 84

3 Answers3

5

In the code behind use this before binding data to GridView (but it's c#):

GV_DataByGroupAct.Columns.Add(new ButtonField() { Text = "Button" });

Or you could prepare the GridView with the button field

    <asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Age" HeaderText="Age" />
            <asp:BoundField DataField="Hight" HeaderText="Hight" />
            <asp:ButtonField Text="Button" />
        </Columns>
    </asp:GridView>

after bind you will have this result:

enter image description here

Jenda Matejicek
  • 151
  • 3
  • 14
  • I've modified the code to use the button field, but the new column does not appear. Code: >> GV_DataByGroupAct.DataSource = dt_AllGroupsSetUp2 >> Dim buttonColumn As New ButtonField >> GV_DataByGroupAct.Columns.Add(ButtonColumn) >> GV_DataByGroupAct.DataBind() – Selrac Sep 05 '13 at 14:05
  • Try to add the ButtonColumn first. – Jenda Matejicek Sep 06 '13 at 06:17
  • And how do I catch its click event? (Thanks a lot in advance) – oo_dev Oct 12 '15 at 20:10
  • 1
    For example: and in the code behind: protected void Button1_Command(object sender, CommandEventArgs e) { // -- DO SOMETHING -- info in e.CommandName and e.CommandArgument } – Jenda Matejicek Oct 13 '15 at 06:09
1

I was really complicating things. Thanks Jenda, it is easier to prepare the grid view. The following workds if it helps someone:

<asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" />
        <asp:BoundField DataField="Hight" HeaderText="Hight" />
        <asp:ButtonField Text="Button" />
    </Columns>
</asp:GridView>

Code:

Dim dt_AllGroupsSetUp2 As New DataTable()
dt_AllGroupsSetUp2.Columns.Add("Name", Type.GetType("System.String"))
dt_AllGroupsSetUp2.Columns.Add("Age", Type.GetType("System.String"))
dt_AllGroupsSetUp2.Columns.Add("Hight", Type.GetType("System.String"))

For i As Integer = 0 To 7
    dt_AllGroupsSetUp2.Rows.Add()
    dt_AllGroupsSetUp2.Rows(i)(0) = "John"
    dt_AllGroupsSetUp2.Rows(i)(1) = 10
    dt_AllGroupsSetUp2.Rows(i)(2) = 70
Next

    GV_DataByGroupAct.DataSource = dt_AllGroupsSetUp2
    GV_DataByGroupAct.DataBind()
Selrac
  • 2,203
  • 9
  • 41
  • 84
0

DataGridViewButtonColumn is intended to be used in DataGridView control.

With GridView you can use ButtonField.

tezzo
  • 10,858
  • 1
  • 25
  • 48