编程方式将ButtonColumn从DataTable添加到Gr

编程方式将ButtonColumn从DataTable添加到Gr

本文介绍了以编程方式将ButtonColumn从DataTable添加到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正如你从下面的代码中看到的那样,GridView的数据源是一个DataTable。我需要用一个按钮在表格中添加一个额外的列。



从下面的代码中,我收到一条错误消息:



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

也返回了以下错误:'New'不能用于声明为'MustInherit'的类。

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

有什么想法?

谢谢

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

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

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

 < asp:GridView ID =GV_DataByGroupActrunat =serverAutoGenerateColumns =False> 
<列>
< asp:BoundField DataField =NameHeaderText =Name/>
< asp:BoundField DataField =AgeHeaderText =Age/>
< asp:BoundField DataField =HightHeaderText =Hight/>
< asp:ButtonField Text =Button/>
< /列>
< / asp:GridView>

绑定后您将得到以下结果:


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:

    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

解决方案

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:

这篇关于以编程方式将ButtonColumn从DataTable添加到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:49