【问题标题】:empty row for datagrid in wpf vb.netwpf vb.net中数据网格的空行
【发布时间】:2018-08-05 13:33:33
【问题描述】:

起初很抱歉英语不好! 我正在使用 vb.net,我想在我的应用程序中使用数据网格 我想使用代码中的一些文本并在 datagrid 中插入一行,我搜索它并找到了这篇文章。 我正在使用这篇文章中的代码: click for that post

此代码将添加一行,但它是空的! tnx !
这是 vb.net 代码:

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    Dim row As String() = New String() {"aaa", "150", "2000", "10520"}
    Dim dmb As New MyProduct
    dmb.ItemNumber = "abc001"
    dmb.ItemDescription = "bla bla bla"
    dmb.ItemQty = 1
    dmb.ItemUnitPrice = 123.45
    grd.Items.Add(dmb)
    timer1.Enabled = True
End Sub
Public Class MyProduct
    Public Property ItemNumber As String
    Public Property ItemDescription As String
    Public Property ItemUnitPrice As Double
    Public Property ItemQty As Integer
End Class

这里是 xaml 代码:

            <DataGrid x:Name="grd">
            <DataGrid.Columns>
                <DataGridTextColumn Width="100" Header="name" />
                <DataGridTextColumn Width="100" Header="desc" />
                <DataGridTextColumn Width="100" Header="unt"  />
                <DataGridTextColumn Width="100" Header="qty"  />
            </DataGrid.Columns>
        </DataGrid>

【问题讨论】:

  • 我真的不知道你卡在哪里了。 Take a look here,您可能对DataGrid 的工作原理有更清晰的认识。
  • @P.Manthe 我使用了相同的代码,但要添加的行是空的!
  • 你定义了DataGrid.ColumnsDataGridTextColumn吗?我使用了相同的代码,并且该行不为空。所以我建议你更新你的问题并显示你的代码。
  • @P.Manthe 我添加了代码。 tnx 的帮助!

标签: wpf vb.net datagrid


【解决方案1】:

我认为您没有定义 Columnproperly。您需要将其绑定到某个属性。否则DataGrid 无法“猜测”要显示的内容。 试试这个代码来定义DataGrid.Columns

<DataGrid.Columns>
    <DataGridTextColumn Header="name" Binding="{Binding ItemNumber}"/>
    <DataGridTextColumn Header="desc" Binding="{Binding ItemDescription}"/>
    <DataGridTextColumn Header="qty" Binding="{Binding ItemQty}"/>
    <DataGridTextColumn Header="unt" Binding="{Binding ItemUnitPrice}"/>
</DataGrid.Columns>

您可以查看:DataGrid tutorial 以获取有关如何使用 DataGrid 的更多说明

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 2013-10-27
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2012-12-28
    • 1970-01-01
    相关资源
    最近更新 更多