【问题标题】:WPF Datagrid does not dispay all valuesWPF Datagrid 不显示所有值
【发布时间】:2013-06-16 07:59:27
【问题描述】:

我有这样的 SQL 表,

 Name   |   03.04.2013   |   05.04.2013   |  07.03.2013  |  09.04.2013 
--------|----------------|----------------|--------------|---------------

我想在数据网格中显示此表。

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Fee", sc2);


DataTable dt = new DataTable();
da.Fill(dt);

datagrid2.ItemsSource = dt.DefaultView;

当我尝试它时,数据网格只显示名称,日期列中的值是空的。(AutoGenerateColumns="true")

我添加了 mainwindows.xaml 源 here 和 xaml 代码 here

【问题讨论】:

  • 这是由列名中的点引起的。使用 AutoGenerateColumns="false",您将明确定义绑定路径为 03.04.2013,即 03 值的 04 属性的 2013 属性。给列名一个不带点的别名可以解决这个问题,但我也希望看到一个更好的解决方案。
  • 但是在我做一些改变之前它昨天还在工作
  • 我想没有更好的解决方案。我用“-”换了点,看来问题解决了谢谢你的回答

标签: c# sql wpf wpfdatagrid


【解决方案1】:

试试这个

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Fee", sc2);
        DataSet ds = new DataSet();
        da.Fill(ds, "DataGridBind");
        datagrid2.DataContext = ds;

在 Xaml 中

<DataGrid Name="datagrid2" AutoGenerateColumns="True" ItemsSource="{Binding Path=DataGridBind}">

【讨论】:

  • 还是同样的问题,昨天还在工作,没有任何意义,我做了一些更改,然后发生了这种情况
  • 您在费用表中有记录吗?
  • 是的,它包括 int 值
  • 也许你改变了连接字符串
  • 请包含 xaml 和 xaml.cs 代码。这样我就可以判断出什么问题了
【解决方案2】:

试试..

string ConString = "Use your database address";
string CmdString = string.Empty;
using (SqlConnection con = new SqlConnection(ConString))
{
    CmdString = "SELECT * FROM Fee";
    SqlCommand cmd = new SqlCommand(CmdString, con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable("Fee");
    sda.Fill(dt);
    datagrid1.ItemsSource = dt.DefaultView;
 }

也可以查看..

【讨论】:

  • 我的问题是关于 WPF 我不能使用 datagridview1.Datasource 和 datagridview.Readonly
【解决方案3】:

您可以尝试将 AutoGenerateColumns 设置为 False 并将绑定添加到自定义列:

<DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False">
   <DataGrid.Columns>                    
         <DataGridTextColumn Header="Name" Binding="{Binding Path=NAME}" IsReadOnly="True" Width="*" />
         <DataGridTextColumn Header="Age" Binding="{Binding Path=AGE}" IsReadOnly="True" Width="*" />
         <DataGridTextColumn Header="Date" Binding="{Binding Path=DATE}" IsReadOnly="True" Width="*" />
   </DataGrid.Columns>
</DataGrid>

您还可以创建一个扩展 IValueConverter 的转换器类,以便在您的数据中放置某种掩码。

【讨论】:

  • 在程序用户中添加列。绑定超过 50 列不会那么难
猜你喜欢
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多