Hi, I have a datagrid created programmatically and itemsource set to a datatable.
I want to Right align columns that have numeric data.
I tried both the events AutoGeneratingColumns and AutoGeneratedColumns, but both throw error at e.Column.Header
System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed.
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
Note that same datagrid name 'dg' is created multiple times and added to the UI, all these grid have the same Event, though I don't think this is an issue.
private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Column.Header.ToString() == "COUNT") { Style s = new Style(); s.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right)); } MessageBox.Show(e.Column.Header.ToString()); }
private create_load_grid()
{
foreach (object obj in objsList)
{
DataGrid dg = new DataGrid();
dg.AutoGeneratingColumn += new System.EventHandler<System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs>(dg_AutoGeneratingColumn);
dg.ItemsSource = dt.DefaultView;
dg.IsReadOnly = true;
}
}
Any help would be great
Thanks,
-srinivas y.
sri