2017-02-27 59 views
0

我的代碼是數據是在數據集可視化可見,但在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;    
} 

存儲過程是

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 [email protected]_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,'[email protected]+',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('[email protected]+') 
    )p where Line_ID='[email protected]_ID 

exec (@query) 
    set @[email protected] 
END 

數據網格定義

<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; 

結果是

Dataset visualizer

Output

回答

2

當數據網格生成用於其的ItemsSource列,它用數據表的列名稱作爲列結合路徑。雖然/符號在DataColumn名稱中有效,但它在綁定路徑中具有特殊含義,如果它是名稱的一部分,則應該轉義。 [80/4]80/4

添加事件處理程序AutoGeneratingColumn事件在XAML:

AutoGeneratingColumn="Dg_OnAutoGeneratingColumn" 

,並修改綁定使用此代碼:

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 + "]"); 
} 
+0

非常感謝你這個代碼工作正常的我 –