Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于数据绑定列表框的显示错误_C#_Wpf_Ms Access_Data Binding_Listbox - Fatal编程技术网

C# 关于数据绑定列表框的显示错误

C# 关于数据绑定列表框的显示错误,c#,wpf,ms-access,data-binding,listbox,C#,Wpf,Ms Access,Data Binding,Listbox,请允许我首先描述错误。 1。显示顺序错误 列表框将显示项目,同时从第二个项目开始,第一个项目显示到最后一个项目,但有四个。最后一项也有同样的问题 2。行丢失错误 当我用一个超过400行的datatable绑定listbox时,第一项将丢失,最后一项也将丢失 然后在此处发布代码 1。使用accdb存储数据 下面是以oledb方式从access数据库获取数据的c#代码。我希望我做对了 public partial class MainWindow : Window { DataSet D

请允许我首先描述错误。

1。显示顺序错误

列表框将显示项目,同时从第二个项目开始,第一个项目显示到最后一个项目,但有四个。最后一项也有同样的问题

2。行丢失错误

当我用一个超过400行的datatable绑定listbox时,第一项将丢失,最后一项也将丢失

然后在此处发布代码

1。使用accdb存储数据

下面是以oledb方式从access数据库获取数据的c#代码。我希望我做对了

public partial class MainWindow : Window
{
    DataSet DsMain = new DataSet();
    OleDbConnection Conn = new OleDbConnection();
    OleDbDataAdapter Dpter = new OleDbDataAdapter();

    public MainWindow()
    {
        InitializeComponent();

        string StartPath = System.Windows.Forms.Application.StartupPath + @"\AltiumDatabase.accdb";
        Conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + StartPath);

        try
        {
            Conn.Open();

            string strcmd = "select ID,TaskType, Status,StatusColor,PN,Type,Owner,Checker,Deadline,StartDate,Tag from TRRecord";
            OleDbCommand cmd = new OleDbCommand(strcmd, Conn);
            Dpter.SelectCommand = cmd;

            Dpter.Fill(DsMain, "TRRecord");

            LbxMain.DataContext = DsMain;//bind to the listbox
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            Conn.Close();
        }
    }
}
2。自定义列表框构建

我希望我能得到所有装订好的宣言

<ListBox Name="LbxMain" Margin="20" ItemsSource="{Binding Path=TRRecord}" HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="1" BorderBrush="Gray" Margin="5">
                    <Grid Background="{Binding Path=StatusColor}">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=ID}"/>
                        <TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding Path=PN}"/>
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Path=Type}"/>
                        <TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding Path=TaskType}"/>
                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Status}"/>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我应该使用InotifyPertyChanged还是ObservableCollection?我只是在填充表之后绑定数据,所以没有任何更改,它们真的重要吗? 如果有人帮忙,我真的很感激。感谢所有读过这篇文章的人


呃…我的英语不是很好。如果有什么地方我描述不清楚,请告诉我。

我想你应该试试下一种装订方法

LbxMain.DataContext = DsMain.Tables["TRRecord"];
此外,在XAML中:

<ListBox Name="LbxMain" Margin="20" ItemsSource="{Binding }" HorizontalContentAlignment="Stretch"> ... </ListBox>
。。。

问候。

呃……我似乎刚刚找到解决这个问题的钥匙。由于每个listboxitem都有一个复杂的内容,我应该给出一个特定的排序规则,这意味着使用ListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription(“ID”,System.ComponentModel.ListSortDirection.Ascending));哈哈…谢谢你的回答。我试过用你的方法设置数据绑定,但没用。在我看来,这一切都是关于如何排序列表框中添加的项目。这里令人困惑的是“it”如何决定控件UI上项目的显示顺序,特别是对于上面显示的具有ID、状态、partnumber和其他属性的数据。我现在给列表框一个新的排序描述,所有项目现在都按照我想要的顺序显示。因此,我认为这里的关键是您应该给出自己的排序描述,而不是如何构建数据绑定。还是谢谢你,期待你的其他想法