c# - 数据不显示在数据网格中

标签 c# sql-server wpf

这是我的代码。 XAML:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding }" Height="200" Name="dataGrid1" Width="200" />

CS:

SqlConnection thisConnection = new SqlConnection(@"Server=(local);Database=Sample_db;Trusted_Connection=Yes;");
thisConnection.Open();    
string Get_Data = "SELECT * FROM emp";  
SqlCommand cmd = thisConnection.CreateCommand();
cmd.CommandText = Get_Data;
SqlDataAdapter sda = new SqlDataAdapter(cmd);               
DataTable dt = new DataTable("emp");
sda.Fill(dt);
// MessageBox.Show(cmd.CommandText);
dataGrid1.ItemsSource = dt.DefaultView;  

它在网格上显示线条。不是实际数据。请帮忙。 提前致谢。

最佳答案

Datatable 不会生成带有 AutoGenerateColumns="False" 的数据网格行.

请将其更改为AutoGenerateColumns="True"或者在 <Datagrid.Columns> 中添加您自己的数据列属性。

关于c# - 数据不显示在数据网格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168934/

相关文章:

sql-server - SSIS执行SQL任务错误 "Single Row result set is specified, but no rows were returned."

c# - 我可以反序列化 JSON 中的特定字段吗?

c# - 卸载 Application Insights nuget 包是否安全?

c# - 如何使用PCM 8kHZ和每个样本16位的Naudio loopbackcapture独占模式

c# - 如何将数据表插入到SQL Server数据库表中?

c# - 在父构造函数中设置窗口所有者时遇到问题

c# - 未捕获 System.Data.SQLite 的 FileNotFoundExceptions

使用临时表的 SQL Server 连接上下文不能在使用 SqlDataAdapter.Fill 调用的存储过程中使用

c# - ColumnSeries 中每个值的不同颜色

wpf - 重用内置 WPF 样式