DataGrids和数据绑定

时间:2016-10-20 21:20:29

标签: c# wpf xaml data-binding wpfdatagrid

我正在使用C#和XAML创建一个显示数据库信息的对话框。窗口或多或少地以它应该的方式运行。初始化时,数据库数据会相应地填充DataGrid

enter image description here

如您所见,还有一个ComboBox用于过滤DataGrid的内容。它有点这样做。当我从ComboBox中选择所需的项目时,它会过滤我的数据库,并使用所有数据创建所选对象类型的List<>。我遇到的问题是无法找到一种方法来填充临时表,其中包含来自List<>的数据以填充DataGrid。最后发生的事情是:

enter image description here

数据库中的元素有正确的行数,当我使用中断运行它时,所有正确的数据都在List<>;但数据没有显示在行中。

我无法在数据库中为结果创建新表,因为DataGrid的数据绑定将不起作用;我已经尝试使用来自List<>通过帮助程序类的信息来重新填充表格...无法解决这个问题。

任何想法和/或帮助都会很棒。

以下是我发布的代码数量: XML数据绑定(绑定路径与数据库中的列一致):

<DataGridTextColumn Header="Size" Binding="{Binding Path=ItemSize }"/>
<DataGridTextColumn Header="Elbow" Binding="{Binding Path=Ell}"/>
<DataGridTextColumn Header="Tee" Binding="{Binding Path=Tee}"/>
<DataGridTextColumn Header="Long-Turn Elbow" Binding="{Binding Path=LngEl}"/>
<DataGridTextColumn Header="Check Valve" Binding="{Binding Path=Chk}"/>
<DataGridTextColumn Header="Butterfly Valve" Binding="{Binding Path=Bfy}"/>
<DataGridTextColumn Header="Gate Valve" Binding="{Binding Path=Gate}"/>
<DataGridTextColumn Header="Alarm Valve" Binding="{Binding Path=Alm}"/>
<DataGridTextColumn Header="Dry Pipe Valve" Binding="{Binding Path=DPV}"/>
<DataGridTextColumn Header="45⁰ Elbow" Binding="{Binding Path=45Ell}"/>
<DataGridTextColumn Header="Tee Run" Binding="{Binding Path=Teerun}"/>
<DataGridTextColumn Header="Coupling" Binding="{Binding Path=Coup}"/>
<DataGridTextColumn Header="Swing Check" Binding="{Binding Path=Swg}"/>

C#:

var newTable = new CEqlTable();

var results = from myRow in vTable.AsEnumerable()
              where myRow.SubCategoryID == GetPipeNumber()
              select myRow;

foreach (DataRow dr in results)
{
     var nextEntry = new CEqlTableRec(
         false, (int)dr[0], (int)dr[1], (double)dr[2], (double)dr[3],
         (double)dr[4], (double)dr[5], (double)dr[6], (double)dr[7],
         (double)dr[8], (double)dr[9], (double)dr[10], (double)dr[11],
         (double)dr[12], (double)dr[13], (double)dr[14], (double)dr[15],
         Convert.ToByte(dr[18]));

    newTable.Add(nextEntry);
}

dgPipeDetail.ItemsSource = newTable;

1 个答案:

答案 0 :(得分:1)

所以从来没有真正获得足够的信息来诊断问题,但在下面我发布了一个非常简单的示例,在后面的代码中绑定到一个集合。我还在主窗口上放了一个按钮。单击该按钮时,我清除以前的数据并使用新数据填充数据网格。将下面的内容与您拥有的内容进行比较。看看它是否给你任何想法。

<强> MainWindow.xaml

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="350" 
        Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <DataGrid ItemsSource="{Binding Classes}"
              CanUserAddRows="False"
              AutoGenerateColumns="False"
              Margin="0,0,220,0.5">
        <DataGrid.Columns>
           <DataGridTextColumn Header="FIRST" Binding="{Binding First}"/>
           <DataGridTextColumn Header="SECOND" Binding="{Binding Second}"/>
           <DataGridTextColumn Header="THIRD" Binding="{Binding Third}"/>
           <DataGridTextColumn Header="FOURTH" Binding="{Binding Fourth}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Swap" 
            HorizontalAlignment="Left" 
            Margin="400,70,0,0" 
            VerticalAlignment="Top" 
            Width="75"
            Command="{Binding SwapCommand}"/>
</Grid>

<强> MainWindow.xaml.cs

namespace DataGridTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            SwapCommand = new RelayCommand(OnExecuteSwap);

            InitializeComponent();

            for(int i = 0; i < 10; i++)
            {
                Classes.Add(
                    new MyClass
                    {
                        First = 10,
                        Second = 20,
                        Third = 30,
                        Fourth = 40
                    });
            }
        }

        private void OnExecuteSwap()
        {
            Classes.Clear();

            for(int i = 0; i < 10; i++)
            {
                Classes.Add(
                    new MyClass
                    {
                        First = 50,
                        Second = 60,
                        Third = 70,
                        Fourth = 80
                    } );
            }
        }

        public ICommand SwapCommand { get; }

        public ObservableCollection<MyClass> Classes { get; } =
            new ObservableCollection<MyClass>();
    }
}

MyClass POCO

namespace DataGridTest
{
    public class MyClass
    {
        public int First { get; set; }
        public int Second { get; set; }
        public int Third { get; set; }
        public int Fourth { get; set; }
    }
}