This is a migrated thread and some comments may be shown as answers.

[Solved] Problems setting columns programmatically

6 Answers 482 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jean-Christophe
Top achievements
Rank 1
Jean-Christophe asked on 06 Mar 2009, 10:37 AM
Hello,

I am creating columns programmatically in the GridView control. As I need to bind them afterwards, I need access to the DataMemberBinding property, so I cannot add columns of type GridViewColumn to the columns collection and I therefore directly add columns of type GridViewDataColumn to the columns collection.

The problem when doing so is that the GridView control seems to have skipped some initialization code. I may be mistaking in my assumptions, but the GridView control does clearly not like that beause lots of things do not work anymore or crash. Sorting for example crashes at the second sort, sort indicators do not appear, column reordering does not work, column grouping works for all columns even if I set it to false for a specific column, setting a specific column Readonly property causes crashes when you attempt to edit a cell, the TextAlignement has no effect, etc... really lots of behaviors crash or do  not work.

Is there a way to manipulate the grid programmatically and have a normal behavior ? Here is an extract of my code:

 

public override void OnApplyTemplate()

 

{

 

base.OnApplyTemplate();

 

 

this.DataGridObject = this.GetTemplateChild(DATAGRID_NAME) as RadGridView;

 

 

this.AttachEventsHandlers();

 

 

this.SetDeferredProperties();

 

 

this.CreateColumns();

 

 

this.HideGroupPanelIfUnnecessary();

 

 

this.ApplySorting();

 

}


 

private void CreateColumns()

 

{

 

List<GridViewColumn> columns = new List<GridViewColumn>(this.Columns.Count);

 

 

if (!this.IsDataGridObjectValid)

 

{

 

return;

 

}

 

this.DataGridObject.Columns.Clear();

 

 

foreach (DataGridColumn column in this.Columns)

 

{

columns.Add(CreateColumn(column));

}

 

this.DataGridObject.Columns.AddRange(columns);

 

}

 


private static GridViewColumn CreateColumn(DataGridColumn column)

 

{

 

if (column == null)

 

{

 

return null;

 

}

 

// We don't set the SortingState here via the property because it has no effect

 

 

// (but a little sort indicator (arrow) is nevertheless anyway drawn in the header cell)

 

 

return new GridViewDataColumn

 

{

HeaderText = column.HeaderText,

DataMemberBinding = column.DataMemberBinding,

Width = column.Width,

UniqueName = column.DataMemberBinding.Path.Path,

};

 

//The following properties currently do not work or cause crashes

 

 

//IsReadOnly = column.ReadOnly,

 

 

//IsSortable = column.AllowSorting,

 

 

//IsGroupable = column.AllowGrouping,

 

 

//TextAlignment = column.TextAlignment,

 

 

//Background = column.Background,

 

}


The exact situation is that I have a class called DataGrid, which has a ControlTemplate defined in themes\generic.xaml. This control is intended to be part of an UI framework because we do not want references to third-party controls in our applications. This DataGrid control therefore exposes among others columns, and it is intended to be used in the Xaml of our applications. When it gets initialized, it looks at its configuration, its own columns collection for example, and it sets the properties of its RadGridView control programmatically accordingly to its own settings, so we really cannot let the GridView control auto-generate its columns or define columns the standard way in Xaml because the client applications have no direct references to Telerik libraries.

We are currently using the trial version till the purchase order has been processed. The version of Telerik.Windows.Controls.GridView.dll is 2008.3.1217.1020.

Thanks in advance !

Jean-Christophe

6 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 10 Mar 2009, 01:28 PM
Hi Jean-Christophe,

I could not manage to reproduce the problem locally. Therefore, I prepared a sample application to demonstrate programmatically adding and setting columns. Please see the attached zip.

It is ok to create columns of type GridViewDataColumn and add them at runtime to the RadGridView columns collection .

In the attached sample, I executed the logic in the Loaded event instead of the OnApplyTemplate override. I believe that this might be the difference and the cause of the problem. I guess you should test your columns adding logic within the loading event.

If this does not help we can investigate the problem further but in such case I will need some code  or a small sample project reproducing the issue. You can attach such a sample project to a new support ticket

Regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
dennis
Top achievements
Rank 1
answered on 17 Mar 2009, 11:47 PM
How i can use SortingState?, i want to sort a column programmaticaly because when i bind the grid doesnt show the data until i click in a header to sort a column., im using q1 2009
0
Jean-Christophe
Top achievements
Rank 1
answered on 18 Mar 2009, 01:23 PM
Thank you very much !!
0
Vinod
Top achievements
Rank 1
answered on 04 Jun 2009, 12:56 PM
thanks. that helped me.


Regards,
Vinod Sardar.
0
Pavel Pavlov
Telerik team
answered on 05 Jun 2009, 09:20 AM
Hi Dennis ,

On your question about sorting state:
Typically you would add  SortDescription  to the RadGridView.SortDescriptions collection in order to perform sorting programmatically.

Regards,
Pavel Pavlov
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jonathan
Top achievements
Rank 1
answered on 01 Jul 2011, 08:26 PM
Hi Pavel,

on the columns sample which part of the column defintiion maps the the underlying property name?  Is is the unique Name?  how would I change the binding manually for example if the column header text was "Last Name" and the bound object had a property of "LastName"?
Tags
GridView
Asked by
Jean-Christophe
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
dennis
Top achievements
Rank 1
Jean-Christophe
Top achievements
Rank 1
Vinod
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Share this question
or