gpt4 book ai didi

c# - WPF 数据绑定(bind) : How can I bind a stored procedure to a Listview

转载 作者:行者123 更新时间:2023-11-30 20:14:09 24 4
gpt4 key购买 nike

我有一个按钮,单击该按钮将在 SQL Server 上运行存储过程,并在同一窗口内的网格中显示结果数据。

在 Windows 窗体世界中,我会创建一个数据表,使用数据适配器来填充它,然后将数据表分配给我的 DataGridView 的 DataSource 属性,噗……这就是我的数据。

我在 WPF 中使用带有 Gridview 的 ListView 尝试了类似的东西,但我似乎无法让它工作。我的 XAML 中有以下内容:



<pre><code><ListView Grid.Row="1" Name="Preview" ItemsSource="{Binding Path=Report}">
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=FormalName}" />
</GridView>
</ListView>
</code></pre>

在我的 C# 代码中

private void CreateReport(object sender, RoutedEventArgs e) 
{
DataTable dt = new DataTable("Report");
SqlConnection cn = new SqlConnection("Data Source=DailyTimesheets;
Initial Catalog=DailyTimesheets;Integrated Security=SSPI");

SqlCommand cm = new SqlCommand("Reports.PayrollHoursInterface", cn);
cm.Parameters.AddWithValue("@PayBatchID", 722);
cm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(cm);
da.Fill(dt);

Preview.DataContext=dt;
}

当我单击按钮(触发 CreateReport 方法)时,我的数据表被填满并分配给 Datacontext,但没有任何显示。

最佳答案

我做了一个示例应用程序,发现您需要用 ListView.View 包围您的 GridView,并将 ItemsSource 设置为 {Binding},如下所示:

<ListView Name="Preview" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=FormalName}" />
</GridView>
</ListView.View>
</ListView>

关于c# - WPF 数据绑定(bind) : How can I bind a stored procedure to a Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/830939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com