VB.net WPF动态数据网格

时间:2016-12-28 09:33:01

标签: wpf vb.net

我有一个例程,用于查询访问数据库,然后将数据填充到数据网格中。

这就是我目前所拥有的:

sql = "SELECT CCC1, CCC2, CCC3, CCC4, CCC5, CCC6, CCC7, CCC8, CCC9, CCC10, CCC11, CCC12, CCC13, CCC14, CCC15, CCC16, CCC17, CCC18, CCC19, CCC20 FROM CablesT " &
           "WHERE (CableID = " & CableIDTextBox.Text & ")"
da = New OleDbDataAdapter(sql, Con)

ds.Tables.Clear() 'this is important to clear the table
da.Fill(ds, "DisplaySpecificCable")
WaterfallchartWindow.CableValuesDataGrid.ItemsSource = ds.Tables("DisplaySpecificCable").DefaultView

我的XAML看起来像这样:

  <DataGrid x:Name="CableValuesDataGrid" HorizontalAlignment="Left" Height="75" Margin="380,35,0,0" VerticalAlignment="Top" Width="1305" MinColumnWidth="20" CanUserReorderColumns="True" EnableColumnVirtualization="False" ColumnWidth="60" AlternatingRowBackground="Beige" VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Parameters"></DataGridTextColumn>
                <DataGridTextColumn Header="1mm²" Binding="{Binding CCC1}"></DataGridTextColumn>
                <DataGridTextColumn Header="1.5mm²" Binding="{Binding CCC2}"></DataGridTextColumn>
                <DataGridTextColumn Header="2.5mm²" Binding="{Binding CCC3}"></DataGridTextColumn>
                <DataGridTextColumn Header="4mm²" Binding="{Binding CCC4}"></DataGridTextColumn>
                <DataGridTextColumn Header="6mm²" Binding="{Binding CCC5}"></DataGridTextColumn>
                <DataGridTextColumn Header="10mm²" Binding="{Binding CCC6}"></DataGridTextColumn>
                <DataGridTextColumn Header="16mm²" Binding="{Binding CCC7}"></DataGridTextColumn>
                <DataGridTextColumn Header="25mm²" Binding="{Binding CCC8}"></DataGridTextColumn>
                <DataGridTextColumn Header="35mm²" Binding="{Binding CCC9}"></DataGridTextColumn>
                <DataGridTextColumn Header="50mm²" Binding="{Binding CCC10}"></DataGridTextColumn>
                <DataGridTextColumn Header="70mm²" Binding="{Binding CCC11}"></DataGridTextColumn>
                <DataGridTextColumn Header="95mm²" Binding="{Binding CCC12}"></DataGridTextColumn>
                <DataGridTextColumn Header="120mm²" Binding="{Binding CCC13}"></DataGridTextColumn>
                <DataGridTextColumn Header="150mm²" Binding="{Binding CCC14}"></DataGridTextColumn>
                <DataGridTextColumn Header="185mm²" Binding="{Binding CCC15}"></DataGridTextColumn>
                <DataGridTextColumn Header="240mm²" Binding="{Binding CCC16}"></DataGridTextColumn>
                <DataGridTextColumn Header="300mm²" Binding="{Binding CCC17}"></DataGridTextColumn>
                <DataGridTextColumn Header="400mm²" Binding="{Binding CCC18}"></DataGridTextColumn>
                <DataGridTextColumn Header="500mm²" Binding="{Binding CCC19}"></DataGridTextColumn>
                <DataGridTextColumn Header="630mm²" Binding="{Binding CCC20}"></DataGridTextColumn>

                </DataGrid.Columns>

        </DataGrid>

这将填充从第2列开始的datagrid行,这是我需要的但我也想:

  1. 在&#34;参数&#34;列,向行添加静态文本。我无法想办法做到这一点。

  2. 通过另一个表的SQL查询向数据网格添加第二行。无法弄清楚这一点。

  3. 非常感谢。

2 个答案:

答案 0 :(得分:0)

据我了解你的问题:你可以试试你的SQL staement

选择“静态文本”AS ParamColumn,CC1,CC2 ...

因此,您可以按照与其他列相同的方式进行绑定。

答案 1 :(得分:0)

关于你的第一点:         sql =&#34; SELECT&#39;您自己的静态代码&#39; CCC0,CCC1,CCC2,CCC3,CCC4,CCC5,CCC6,CCC7,CCC8,CCC9,CCC10,CCC11,CCC12,CCC13,CCC14,CCC15,CCC16,CCC17,CCC18,CCC19,CCC20来自CablesT&#34; &安培;            &#34; WHERE(CableID =&#34;&amp; CableIDTextBox.Text&amp;&#34;)&#34;         da =新的OleDbDataAdapter(sql,Con)

    ds.Tables.Clear() 'this is important to clear the table
    da.Fill(ds, "DisplaySpecificCable")
    WaterfallchartWindow.CableValuesDataGrid.ItemsSource = ds.Tables("DisplaySpecificCable").DefaultView

然后

    <DataGridTextColumn Header="Parameters"  Binding="{Binding CCC0}"></DataGridTextColumn>
            <DataGridTextColumn Header="1mm²" Binding="{Binding CCC1}"></DataGridTextColumn>

这就是我理解你的问题的方式。希望它有用。