0

my code is

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

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;

result is

Dataset visualizer

Output

2 Answers 2

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

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 + "]");
}
4
  • i have the related issue "Syntax error in PropertyPath 'Unmatched bracket" how can i write the same which will work with any of the special charactes
    – Uday Teja
    Nov 28, 2018 at 10:13
  • @UdayTeja, take a look at the DataTable expression syntax in the docs and escape special symbols in your names (search for "it must be escaped" part in the article)
    – ASh
    Nov 28, 2018 at 11:20
  • thankyou @ASh now it is working in most of the cases. except ^ (carat) can you help me with this
    – Uday Teja
    Nov 28, 2018 at 12:41
  • @UdayTeja, please ask a separate question with a minimal reproducible example
    – ASh
    Nov 28, 2018 at 17:23
0

I had the same issue but found the above answer not quite sufficient. Wrapping the Binding.Path fixes interactions with the user, but not saving the changes back to the underlying bound DataTable/DataRowView

This was resolved by calling this from OnAutogeneratingColumn method:

    private void wrapBinding(DataGridAutoGeneratingColumnEventArgs e)
    {
        DataGridTextColumn textColumn = e.Column as DataGridTextColumn;
        Binding binding = textColumn?.Binding as Binding;
        if (binding != null)
        {
            binding.Path.Path = wrapName(binding.Path.Path);
            textColumn.SortMemberPath = wrapName(binding.Path.Path);
            binding.BindingGroupName= wrapName(binding.Path.Path);
        }
        string wrapName(string name) => $"[{name}]";
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.