vb.net 以编程方式将 ButtonColumn 从 DataTable 添加到 GridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18636376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:57:07  来源:igfitidea点击:

Programmatically Add ButtonColumn to GridView From DataTable

asp.netvb.netgridviewdatatable

提问by Selrac

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

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

从下面的代码中可以看出,GridView 的数据源是一个 DataTable。我需要使用按钮向表格中添加一列。

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

“System.Windows.Forms.DataGridViewButtonColumn”类型的值无法转换为“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'.

我也尝试了以下操作,但返回了以下错误:“New”不能用于声明为“MustInherit”的类。

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

Any ideas?

有任何想法吗?

Thanks

谢谢

回答by Jenda Matejicek

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

在将数据绑定到 GridView 之前,在后面的代码中使用它(但它是 c#):

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

Or you could prepare the GridView with the button field

或者您可以使用按钮字段准备 GridView

    <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

在此处输入图片说明

回答by Selrac

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

我真的把事情复杂化了。感谢 Jenda,准备网格视图更容易。如果对某人有帮助,以下内容有效:

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

回答by tezzo

DataGridViewButtonColumn is intended to be used in DataGridView control.

DataGridViewButtonColumn 旨在用于 DataGridView 控件。

With GridView you can use ButtonField.

使用 GridView,您可以使用ButtonField