【问题标题】:Binding List<T> to Datagridview when DGV has specific column types当 DGV 具有特定列类型时,将 List<T> 绑定到 Datagridview
【发布时间】:2013-03-04 19:05:17
【问题描述】:

我正在尝试从列表中填充 DGV。 Datagridview 是可编辑的,并指定了特定的列类型。即文本框、组合框、复选框。

如果我使用 DGV.Datasource = MyList,它会将我的 T 的所有属性添加到 DGV 指定的列之后。 (例如,地址 1、县、市、ID、地址 1、县、市

Addresses = New List(Of Address)

    Dim a1 = New Address(Guid.NewGuid())
    a1.Address1 = "Address 1"
    a1.County = "County Test"
    a1.City = "My City"

    Addresses.Add(a1)

    Dim a2 = New Address(Guid.NewGuid())
    a2.Address1 = "Address 1"
    a2.County = "County Test"
    a2.City = "My City"

    Addresses.Add(a2)

    Dim a3 = New Address(Guid.NewGuid())
    a3.Address1 = "Address 1"
    a3.County = "County Test"
    a3.City = "My City"

    Addresses.Add(a3)
    uxAddresses.DataSource = Addresses

如果我循环遍历,如下所示,它将第一行设置为正常,但不是第 2 行或第 3 行(在我的测试对象中)。

Private Sub DataGridViewPaint(ByVal la As List(Of Address),
                              ByVal paramDgv As DataGridView)
    Dim intDr As Integer = 0

    For Each g In la
        paramDgv.Rows(intDr).Cells("ID").Value = g.Id
        paramDgv.Rows(intDr).Cells("Address1").Value = g.Address1
        paramDgv.Rows(intDr).Cells("County").Value = g.County
        paramDgv.Rows(intDr).Cells("City").Value = g.City
        intDr += 1
    Next
End Sub

这让我想知道,完成此任务的正确方法是什么,因为我觉得我在这里遗漏了一些东西......谢谢

【问题讨论】:

    标签: .net vb.net winforms datagridview


    【解决方案1】:

    您不必一次将列表添加到网格视图中的一行。您可以将整个列表绑定到网格视图,如下所示:

    <asp:GridView ID="GridView1" runat="server" AllowSorting="False"
      AutoGenerateColumns="false" BackColor="White" 
      BorderWidth="2px" BorderStyle="Solid"
      CellPadding="4" ForeColor="#333333" GridLines="both" 
      EmptyDataText="No Log Messages">
    
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
         <Columns>                                  
           <asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderText="ID" HeaderStyle-ForeColor="white">
             <ItemTemplate>
                <asp:Label ID="lblId" runat="Server" Text=' <%#Eval("ID")%>' />
                 </ItemTemplate>
            </asp:TemplateField>  
    <asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderText="Address" HeaderStyle-ForeColor="white">
             <ItemTemplate>
                <asp:Label ID="lblAddress" runat="Server" Text=' <%#Eval("Address1")%>' />
                 </ItemTemplate>
            </asp:TemplateField>                   
         </Columns>
     </asp:GridView>
    

    那么就绑定源码吧:

     Addresses = New List(Of Address)
    'file the list
    
    me.GridView1.dataSource = Addresses 
    me.GridView1.DataBind()
    

    只需确保您在客户端网格视图中的代码: 匹配对象列表中的属性名称。

    【讨论】:

    • Appologies,我忘了指明这是一个 winforms 应用程序。所以我没有可用的.DataBind()。不过还是谢谢。
    • 如果你只是 me.GridView1.dataSource 而没有数据绑定会发生什么?这行得通吗?
    【解决方案2】:

    您可以尝试像这样使用DataGridViewColumn.DataPropertyName 属性:

    Column1.DataPropertyName = "Address1"
    Column2.DataPropertyName = "County"
    Column3.DataPropertyName = "City"
    Column4.DataPropertyName = "ID"
    

    然后,设置DGV.Datasource = MyList

    补充说明:您需要设置每一列的DataPropertyName 字段,否则您可能会得到重复的列。

    【讨论】:

    • 这实际上就像一个魅力,除了我仍然得到重复/额外的列。并非每一列都是重复的,它会在我在设计器中指定的列之后显示它们
    • @RefractedPaladin:确保在为每一列设置DGV.DataSource 之后设置DataPropertyName。否则,我相信你提到的症状会出现。
    • 我做到了。如果我将每个属性都指定为列,它就会消失。
    • @RefractedPaladin:很好……我已经根据 cmets 更新了我的答案,以防其他人偶然发现这一点。谢谢。
    猜你喜欢
    • 2017-05-18
    • 2010-09-12
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多