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

Show text instead of checkbox in RadGridView

10 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 13 Nov 2013, 02:08 PM
We need our RadGridView to simply show text (e.g., True/False) instead of a checkbox for boolean values. The grid is read-only, we're binding it to a DataView, and we need to continue to let it auto-generate the columns at runtime.

I assume we just need to prevent the grid from using a GridViewCheckBoxColumn for boolean-typed columns when it auto-generates its columns. What's the best way to do that, with performance being really important? I've tried overriding templates and styles to no avail. Right now I have it working by using a style that overrides the CheckBox's control template and puts a TextBlock there, using a boolean-to-string converter we wrote, but that's not ideal. I really hope I missed something simple!

BTW, the main reason we want to NOT show checkboxes is performance -- we've found that a lot of checkboxes really slow down rendering of the grid. If that problem has been solved, we might leave the checkboxes in there, though I'd still like the option to just show text instead.

Thanks.

10 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 18 Nov 2013, 11:37 AM
Hi Phil,

You can use a simple DataColumn with a binding converter in order to achieve what you are looking for. 

Hope this helps! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Phil
Top achievements
Rank 1
answered on 18 Nov 2013, 03:37 PM
Would you explain what you mean by "use a simple DataColumn", or maybe provide a code snippet? The grid's AutoGenerateColumns is True; we need that because we don't know what the data will be at design-time.
0
Nick
Telerik team
answered on 19 Nov 2013, 07:25 AM
Hi Phil,

You can use the AutoGeneratingColumn event to see what columns are being created, and replace the e.Column property to GridViewDataColumn, when a CheckBoxColumn is generated. 

Hope this helps! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Nick
Telerik team
answered on 19 Nov 2013, 07:25 AM
Hi Phil,

You can use the AutoGeneratingColumn event to see what columns are being created, and replace the e.Column property to GridViewDataColumn, when a CheckBoxColumn is generated. 

Hope this helps! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Phil
Top achievements
Rank 1
answered on 19 Nov 2013, 02:34 PM
Hi, Nik.

That solution was our first idea as it seems the logical way to do it. However, in the AutoGeneratingColumns event the incoming e.Column is never of type GridViewCheckBoxColumn, it is always a GridViewDataColumn, even for the column whose data type is boolean (non-nullable bit in SQL Server), and the grid always places a checkbox for that column in the grid. (We're using RadControls for WPF Q3 2013.)
0
Dimitrina
Telerik team
answered on 19 Nov 2013, 02:50 PM
Hi,

Indeed, you are right. You cannot change the column just changing this.
You will need to apply a converter to convert the Boolean value to a String one.
For example:
private void clubsGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    var dataColumn = e.Column as GridViewDataColumn;
    if (dataColumn.Header.ToString() == "IsActive")
    {
        GridViewDataColumn newColumn = new GridViewDataColumn();
 
        Binding bb = new Binding();
        bb.Converter = new MyConv();
        bb.Path = dataColumn.DataMemberBinding.Path;
        newColumn.DataMemberBinding = bb;
        newColumn.Header = dataColumn.Header;
        newColumn.UniqueName = dataColumn.UniqueName;
        e.Column = newColumn;
    }
}

Let me know how this works for you.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Phil
Top achievements
Rank 1
answered on 19 Nov 2013, 04:06 PM
That helped indeed. Since the e.Column was already a GridViewDataColumn, I assumed setting it to another of the same type would have no affect, but apparently it does. I tweaked your sample to fit our needs (including our customer converter) and it works fine. Thanks.
Type type = e.ItemPropertyInfo.PropertyType;
// If the type is generic it means it's nullable, so get the underlying value type
if (type.IsGenericType) type = type.GetProperty("Value").PropertyType;
 
// If appropriate, change what would be a checkbox column to a regular text column (for better performance)
if (type == typeof(bool) && Grid.IsReadOnly)
{
    e.Column = new GridViewDataColumn
    {
        DataMemberBinding = new Binding
        {
            Source = col.DataMemberBinding.Source,
            Path = col.DataMemberBinding.Path,
            Converter = _booleanToStringConverter,
            ConverterParameter = "True;False"
        },
        Header = col.Header,
        UniqueName = col.UniqueName,
        DataFormatString = col.DataFormatString
    };
}
0
Accepted
Dimitrina
Telerik team
answered on 19 Nov 2013, 04:13 PM
Hi,

I am glad to hear that.

I can even suggest you this solution, without creating a new instance of a column:
private void clubsGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    var dataColumn = e.Column as GridViewDataColumn;
    if (dataColumn.Header.ToString() == "IsActive")
    {
        dataColumn.DataMemberBinding = new Binding
            {
                Source = dataColumn.DataMemberBinding.Source,
                Path = dataColumn.DataMemberBinding.Path,
                Converter = _booleanToStringConverter,
                ConverterParameter = "True;False"
            };
    }
}


Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Phil
Top achievements
Rank 1
answered on 19 Nov 2013, 04:30 PM
OK. Actually, assigning a new binding is apparently all that's needed.
col.DataMemberBinding = new Binding
{
    Source = col.DataMemberBinding.Source,
    Path = col.DataMemberBinding.Path,
    Converter = _booleanToStringConverter,
    ConverterParameter = "True;False"
};
0
Dimitrina
Telerik team
answered on 19 Nov 2013, 04:32 PM
Hello,

Indeed.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Phil
Top achievements
Rank 1
Answers by
Nick
Telerik team
Phil
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or