繁体   English   中英

如何将数据集中的所有表绑定到数据网格

[英]How can I bind all the tables in a DataSet to a DataGrid

我正在尝试将每个DataSet的表绑定到自己的DataGrid ,但是不确定如何执行此操作,这是我尝试过的操作:

<ListView ItemsSource="{Binding CalibrationData.CalibrationValuestoWrite}">

    <DataGrid ItemsSource="{Binding}">
    </DataGrid>

</ListView>

也许我需要创建一个DataTemplate 任何帮助是极大的赞赏!

试试这个希望对您有帮助

<DataGrid AutoGenerateColumns="False" Height="250" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="12,40,0,0" Name="mytbl" VerticalAlignment="Top" Width="479">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Col1" Width="50" />
                <DataGridTextColumn Header="Col2" Width="375"/>
                <DataGridTextColumn Header="Col3" Width="50"/>
            </DataGrid.Columns>
        </DataGrid>

后面的代码

this.mytbl.DataContext = ds.Tables[0].DefaultView; 

您只能将ItemsSource属性绑定到IEnumerable 然后定义一个ItemTemplate来定义可枚举返回的每个项目的外观。

因此,如果CalibrationData.CalibrationValuestoWrite返回IEnumerable<DataTable>DataSet.Tables属性确实如此),并且您希望为每个DataTable显示一个DataGrid ,则该方法应该起作用:

<ListView ItemsSource="{Binding CalibrationData.CalibrationValuestoWrite.Tables}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <DataGrid ItemsSource="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

最好像下面的这个小示例那样与DataView绑定:

(vm是XAML视图的数据上下文)。

XAML:

<DataGrid ItemsSource="{Binding MyGrid}">
</DataGrid>

C#,ViewModel

    public partial class vm : INotifyPropertyChanged
    {
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string text)
      {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(text));
        }
      } 

      public vm()
      {
        DataTable dt;

        ...

        MyGrid = new DataView(dt);
      }

      private DataView _mygrid;
      public DataView MyGrid
      {
         get
         {
            return _mygrid;
         }
         set
         {
            _mygrid= value;
            OnPropertyChanged("MyGrid");
         }
      }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM