DataGrid绑定DataTable出错

Posted xpvincent

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataGrid绑定DataTable出错相关的知识,希望对你有一定的参考价值。

直接用DataGrid.ItemSource = DataTable.DefaultView时会出现以下错误:

target element is ‘TextBlock‘ (Name=‘‘); target property is ‘Text

 

需要用CollectionViewSource来做中间转换 .另外实时更新数据,所以不能把它做成StaticResource。

<Grid Margin="5">
        <TabControl>
            <TabItem Name="TabInfo" Header="信息" IsSelected="True" />
            <TabItem Name="TabPlot" Header="画图" IsSelected="True" />
            <TabItem Name="TabData" Header="数据" IsSelected="True" >
                <Grid Margin="5">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <toolkit:DataGrid VirtualizingStackPanel.VirtualizationMode="Recycling" 
                                      VirtualizingStackPanel.IsVirtualizing="True" 
                                      EnableColumnVirtualization="True"
                                      EnableRowVirtualization="True"
                                      RowHeight="23"
                                      ScrollViewer.HorizontalScrollBarVisibility="Visible"
                                      ScrollViewer.VerticalScrollBarVisibility="Visible"
                                      Name="DgdData" Grid.Row="0" AutoGenerateColumns="True" DataContext="{Binding DataTableSource}" ItemsSource="{Binding}" />
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0" Name="TxtPageSize" Width="60" Height="26" Margin="0 10 0 10" Text="{Binding DataPageSize}" />
                        <TextBox Grid.Column="0" Name="TxtPageNum" Width="60" Height="26" Margin="0 10 0 10" Text="{Binding DataPageNum}" />
                        <Button Grid.Column="2" Name="BtnFirstPage" Content="第一页"  Width="60" Height="26" Margin="10 0 0 0" Click="BtnFirstPage_Click" />
                        <Button Grid.Column="3" Name="BtnPrevPage" Content="上一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnPrevPage_Click" />
                        <Button Grid.Column="4" Name="BtnNextPage" Content="下一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnNextPage_Click" />
                        <Button Grid.Column="5" Name="BtnLastPage"  Content="最后一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnLastPage_Click" />
                    </Grid>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
public void ShowData(VmPlotData data)
        {
            string title;
            this.mData = data;
            this.DataContext = data;

            this.mDtData = new DataTable();
            title = "Time (" + (this.mData.TimeUnit == string.Empty ? "-" : this.mData.TimeUnit) + ")";
            this.mDtData.Columns.Add(new DataColumn(title, typeof(double)));


            foreach (VmDataRequest req in this.mData.Requests)
            {
                foreach (VmDataComponent cmp in req.Components)
                {
                    title = req.Name + ":" + cmp.Name + "(" + (cmp.Unit == string.Empty ? "-" : cmp.Unit) + ")";
                    this.mDtData.Columns.Add(new DataColumn(title, typeof(double)));
                }
            }

            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);

            this.BtnFirstPage.IsEnabled = true;
            this.BtnLastPage.IsEnabled = true;
            this.BtnPrevPage.IsEnabled = true;
            this.BtnNextPage.IsEnabled = true;
        }

        private void BtnFirstPage_Click(object sender, RoutedEventArgs e)
        {
            this.mData.DataPageNum = 1;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnPrevPage_Click(object sender, RoutedEventArgs e)
        {
            this.mData.DataPageNum--;
            this.mData.DataPageNum = this.mData.DataPageNum > 1 ? this.mData.DataPageNum : 1;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnNextPage_Click(object sender, RoutedEventArgs e)
        {
            int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize);
            this.mData.DataPageNum++;
            this.mData.DataPageNum = this.mData.DataPageNum > maxPageNum ? maxPageNum : this.mData.DataPageNum;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnLastPage_Click(object sender, RoutedEventArgs e)
        {
            int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize);
            this.mData.DataPageNum = maxPageNum;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }
        
        private void SetDisData(int pageNum, int pageSize)
        {
            DataRow row;
            int startRow = (pageNum - 1) * pageSize;
            int endRow = startRow + pageSize;
            endRow = this.mData.Times.Count > endRow ? endRow : this.mData.Times.Count;
            this.mDtData.Rows.Clear();


            foreach (VmDataRequest req in this.mData.Requests)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    row = this.mDtData.NewRow();
                    row[0] = this.mData.Times[i];

                    for (int j = 0; j < req.Components.Count; j++)
                    {
                        row[j+1] = req.Components[j].Data[i];
                    }

                    this.mDtData.Rows.Add(row);
                }
            }

            CollectionViewSource itemCollectionViewSource2 = new CollectionViewSource();
            itemCollectionViewSource2.Source = this.mDtData;
            DgdData.DataContext = itemCollectionViewSource2;

        }

 

以上是关于DataGrid绑定DataTable出错的主要内容,如果未能解决你的问题,请参考以下文章

如何将 DataTable 绑定到 Datagrid

将 DataGrid 绑定到两个 DataTable

在 DataTable 更改 WPF 后更新绑定到 DataTable 的 DataGrid

使用 TemplateColumns 将 WPF DataGrid 绑定到 DataTable

DataTable 作为 DataGrid.ItemsSource

WPF DataGrid - 如何暂停数据绑定中的 UI 更新并稍后进行批量更新