Boolean column as image

1 Answer 68 Views
GridView
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Giovanni asked on 17 Nov 2023, 10:35 AM

Hi, I need to show a boolean column in a RadGridView.

I would like to show the column as in the attached image, i.e. with a tick for the true value and nothing for the false value.

I would like to use GridViewCheckBoxColumn but, this column always shows values as checkboxes. In my case, the table is readonly, so the standard checkboxed makes the user to think that he can click to change the check value.

I tried using a GridViewTextBoxColumn and using CellFormatting event:

 


private void radGrid_CellFormatting(object sender, CellFormattingEventArgs e)
{
      if (this.DesignMode) return;

			if (e.CellElement.ColumnInfo.FieldName == "ScambioPratiche") {
				if ((bool)((System.Data.DataRowView)e.CellElement.RowInfo.DataBoundItem).Row["ScambioPratiche"] == true)
				{
					e.CellElement.Image = MyApp.Main.Properties.Resources.tick;
				}
				else
				{
					e.CellElement.Image = null;
				}
				e.CellElement.Text = string.Empty;
			}
			else
			{
				e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
			}

		}

 

In this way, however, the filter is a text filter and not a checkboxfilter...
Is there a better way to gain my goal, possibly by GridViewCheckBoxColumn ?

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 17 Nov 2023, 11:22 AM

Hello, Giovanni,

GridViewCheckBoxColumn gets automatically created when the data is of type Boolean. In order to use a custom image for the checkmarks you can use the GridCheckBoxCellElement in the CellFormatting event. Please refer to the following code snippet:

private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridCheckBoxCellElement checkboxcell= e.CellElement as GridCheckBoxCellElement;
    if (checkboxcell != null)
    {
        RadCheckBoxEditor editor = checkboxcell.Editor as RadCheckBoxEditor;
        RadCheckBoxEditorElement el = editor.EditorElement as RadCheckBoxEditorElement;

        if (el.CheckState == Telerik.WinControls.Enumerations.ToggleState.On)
        {
            el.Checkmark.Visibility = ElementVisibility.Collapsed;
            e.CellElement.Image = Image.FromFile(@"..//..//check-mark.png");
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
        }
    }
}

Should you have any other questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
commented on 17 Nov 2023, 01:23 PM

Thank you, it works as I hoped!
Tags
GridView
Asked by
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or