本文介绍了数据在DataSet可视化器中可见,但不显示在DataGrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

public DataTable Load_to_DataGrid(string Line_ID)
{
    con.ConnectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
    com = new SqlCommand("LoadPoints_ToGrid", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.Add(new SqlParameter("@Line_ID", Line_ID));
    com.Parameters.Add("@outp", SqlDbType.NVarChar,40000).Direction = ParameterDirection.Output;
    SqlDataAdapter sda = new SqlDataAdapter(com);
    DataTable dt = new DataTable();

    sda.Fill(dt);
    return dt;            
 }






存储的Pros是


Stored Proc is

CREAT PROCEDURE [dbo].[LoadPoints_ToGrid](@Line_ID nvarchar(max),@outp nvarchar(max) output)       
AS
BEGIN
DECLARE @cols AS NVARCHAR(MAX),
      @query  AS NVARCHAR(MAX)
        select @cols=STUFF((select ','+ QUOTENAME(Thickness.Thicknes_Date) from Thickness
     where Line_ID=@Line_ID group by Thicknes_Date order by Thicknes_Date for XML path(''),TYPE ).
     value('.', 'NVARCHAR(MAX)') ,1,1,'')

    if (@Line_ID!='0')
        set @query = 'select Point_NO,'+@cols+',ST_CR,LG_CR from (select Thickness.Line_ID,Thicknes_Date,Points.Point_NO,Points.Point_Val,Points.ST_CR,Points.LG_CR from Thickness inner join Points on Thick_ID=Thicknes_ID) x 
   pivot 
   (
    sum(Point_Val)
    for Thicknes_Date in('+@cols+')
   )p where Line_ID='+@Line_ID

exec (@query)
    set @outp=@query
END






数据网格定义


Data grid definition

<DataGrid x:Name="PointsDbGrid" Grid.Row="2" AutoGenerateColumns="True" ItemsSource="{Binding}" AlternatingRowBackground="#FFDCE890"   Margin="0,10,0,5" ColumnHeaderStyle="{StaticResource HeaderStyle}" ScrollViewer.CanContentScroll="True" ColumnWidth="70"  >
    <DataGrid.ContextMenu>

        <ContextMenu>
            <MenuItem Header="New Thickness" Click="MenuItem_Click">
                <MenuItem.Icon>
                    <Image Source="Images/Add.png" Stretch="Fill"/>
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>







PointsDbGrid.DataContext = obj.Load_to_DataGrid(LineID).DefaultView;
//   PointsDbGrid.ItemsSource = obj.Load_to_DataGrid(LineID).DefaultView;

结果为

推荐答案

当DataGrid为它的ItemsSource生成列时,它将使用DataTable列名称作为列的绑定路径。虽然 / 符号在DataColumn名称中有效,但in在绑定路径中具有特殊含义,如果它是名称的一部分,则应将其转义。 [80/4] 不是 80/4

When DataGrid generates columns for its ItemsSource, it uses DataTable column names as binding path for columns. While / symbol is valid in DataColumn names, in has a special meaning in binding path and should be escaped if it is a part of name, e.g. [80/4] not 80/4

将事件处理程序添加到xaml中的 AutoGeneratingColumn 事件:

add event handler to AutoGeneratingColumn event in xaml:

AutoGeneratingColumn="Dg_OnAutoGeneratingColumn"

并使用以下代码修改绑定:

and modify bindings with this code:

private void Dg_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var textCol = e.Column as DataGridTextColumn;
    if (textCol == null)
        return;
    var binding = textCol.Binding as Binding;
    if (binding == null)
        return;
    binding.Path = new PropertyPath("[" + binding.Path.Path + "]");
}

这篇关于数据在DataSet可视化器中可见,但不显示在DataGrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:15