Hello;
In your WPF control it looks like you can do this:
https://www.telerik.com/forums/how-to-add-columngroup-in-radgridview-columngroups-at-run-time-from-code-behind
How can I add a new columnGroup to the current ViewDefinition?
Thank you,
Joe
5 Answers, 1 is accepted
I seem to *almost* have it working with this:
ColumnGroupsViewDefinition view =
new
ColumnGroupsViewDefinition();
view = (ColumnGroupsViewDefinition)transactions_grid.MasterTemplate.ViewDefinition;
view.ColumnGroups.Add(
new
GridViewColumnGroup(
"New Group"
));
view.ColumnGroups[0].Rows.Add(
new
GridViewColumnGroupRow());
GridViewTextBoxColumn newColumn =
new
GridViewTextBoxColumn(
"holder"
);
newColumn.HeaderText =
"Place Holder"
;
transactions_grid.Columns.Add(newColumn);
view.ColumnGroups[0].Rows[0].ColumnNames.Add(
"holder"
);
transactions_grid.ViewDefinition = view;
But I get a
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
KeyNotFoundException: The given key was not present in the dictionary.
thrown on the Application.Run(new RadForm1())
??
I almost have it working with the following code:
private
void
add_subgroup_btn_Click(
object
sender, EventArgs e)
{
ColumnGroupsViewDefinition view =
new
ColumnGroupsViewDefinition();
view = (ColumnGroupsViewDefinition)transactions_grid.MasterTemplate.ViewDefinition;
view.ColumnGroups.Add(
new
GridViewColumnGroup(
"New Group"
));
view.ColumnGroups[0].Rows.Add(
new
GridViewColumnGroupRow());
GridViewTextBoxColumn newColumn =
new
GridViewTextBoxColumn(
"holder"
);
newColumn.HeaderText =
"Place Holder"
;
transactions_grid.Columns.Add(newColumn);
view.ColumnGroups[0].Rows[0].ColumnNames.Add(
"holder"
);
transactions_grid.ViewDefinition = view;
}
But I get a
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at Telerik.WinControls.UI.RadGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Flex_Billing_Console.Program.Main() in E:\Local Source Control\DDP Flex\Flex Billing Console\Flex Billing Console\Program.cs:line 17
Inner Exception 1:
KeyNotFoundException: The given key was not present in the dictionary.
private void add_subgroup_btn_Click(object sender, EventArgs e)
{
ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
view = (ColumnGroupsViewDefinition)transactions_grid.MasterTemplate.ViewDefinition;
view.ColumnGroups.Add(new GridViewColumnGroup("New Group"));
view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
GridViewTextBoxColumn newColumn = new GridViewTextBoxColumn("holder");
newColumn.HeaderText = "Place Holder";
transactions_grid.Columns.Add(newColumn);
view.ColumnGroups[0].Rows[0].ColumnNames.Add("holder");
transactions_grid.ViewDefinition = view;
}
But I get a
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at Telerik.WinControls.UI.RadGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Flex_Billing_Console.Program.Main() in E:\Local Source Control\DDP Flex\Flex Billing Console\Flex Billing Console\Program.cs:line 17
Inner Exception 1:
KeyNotFoundException: The given key was not present in the dictionary.
Thank you for writing.
If you need to add a new column to the applied ColumnGroupsViewDefinition of RadGridView, it is necessary to create a new instance of the ColumnGroupsViewDefinition and add all of the existing columns together with the new column. Then, apply this new instance of the ColumnGroupsViewDefinition to the RadGridView.ViewDefinition property.
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Progress Telerik