0

I am binding my Data Table to a data grid via ItemsSource. If any column name contains either a left or a right parenthesis, it's throwing some exception i.e

An error occurred while dispatching a call to the UI Thread

am using MVVM Pattern.

Here is the Xaml Code

<DataGrid x:Name="bindedItems" Grid.Row="1" ItemsSource="{Binding BindedItems}" SelectionMode="Extended"/>

Here is the C# Code

this.BindedItems = new DataTable();
this.BindedItems = table;

table columns are like this

Right Parenthesis)

testing

Lest Parenthesis(

and from that column skipping remaining columns.

How to handle these type of exceptions and how to skip these type of columns from UI?

Please give me any suggestion to handle these things.

raghava arr
  • 183
  • 1
  • 14

1 Answers1

0

Set the AutoGenerateColumns property to false and explicitly define only the columns that you want to show:

<DataGrid x:Name="bindedItems" Grid.Row="1" ItemsSource="{Binding BindedItems}" SelectionMode="Extended" 
                  AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
    </DataGrid.Columns>
</DataGrid>

Or rename your columns:

foreach(DataColumn col in table.Columns)
{
    col.ColumnName = col.ColumnName.Replace("(", "").Replace(")", "");
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • different type of tables am binding i.e, every table having different type of columns. In that what should i do – raghava arr Sep 19 '17 at 10:19
  • Then you will have to change the column names. It makes no sense to have a single parenthesis in a column name. Anyway, these are your (only) options. – mm8 Sep 19 '17 at 10:20
  • one more thing for every table having different no. of columns – raghava arr Sep 19 '17 at 10:22
  • if i changed every column name manually it seems to be bad coding – raghava arr Sep 19 '17 at 10:23
  • Nothing can be worse than having parenthesis in a column name...that's bad coding standards if any. It's so bad that it doesn't event work :) – mm8 Sep 19 '17 at 10:24
  • clients can create any type of column names then we have to handle these type of things. – raghava arr Sep 19 '17 at 10:28
  • As already mentioned, the DataGrid does not support these kind of names. – mm8 Sep 19 '17 at 10:29
  • I understood that, How can i handle that in c#. If you have any suggestion or solution – raghava arr Sep 19 '17 at 10:31
  • I have already given you the options in my answer. If neither of this works for you, you cannot use the DataGrid control. – mm8 Sep 19 '17 at 10:31
  • if the column name will be like this i.e, "((Testing)". the above code removes all parenthesis – raghava arr Sep 19 '17 at 10:33
  • Yes, so why don't you remove all parenthesis? As mentioned, a column name shouldn't include parenthesis. But the code was just an example. The main point is that you need to rename the columns. If don't know how to remove any single (, you should ask a new question. – mm8 Sep 19 '17 at 10:35