We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Button condition Styling in Datagrid

I am trying to have a different image for each button based on conditional statements. My code works fine, but its rather slow and I'm not sure why. In datagridviews you could achieve this fairly easily with the CellPaint, but I believe we have to do the QueryButtonCellStyle for formatting.

private void sfDataGrid1_QueryButtonCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryButtonCellStyleEventArgs e)
{
    //Finds the column with
    if (e.Column.MappingName.Equals("Open"))
    {
        //if the project exists then have an open icon else have a different icon
        if (Directory.Exists(projectFolderPath))
        {
            e.Button.Image = Properties.Resources.open.ToBitmap();
            e.Button.ImageSize = new System.Drawing.Size(w, h);
            e.Column.CellStyle.HorizontalAlignment = HorizontalAlignment.Center;
        }
        else
        {
            e.Button.Image = Properties.Resources.failedopen.ToBitmap();
            e.Button.ImageSize = new System.Drawing.Size(w, h);
            e.Column.CellStyle.HorizontalAlignment = HorizontalAlignment.Center;
        }
    }
    //Another button with another icon
    if (e.Column.MappingName.Equals("Map"))
    {
        var w = Properties.Resources.edit.Width / 4;
        var h = Properties.Resources.edit.Height / 4;
        e.Button.Image = Properties.Resources.map.ToBitmap();
        e.Button.ImageSize = new System.Drawing.Size(w, h);
        e.Column.CellStyle.HorizontalAlignment = HorizontalAlignment.Center;
    }
}

When I run this, my datagrid shows the right icons, but it flickers in and out because its running through the code again and again because of the CellStyles. Let me know if there's a better way of doing this.

1 Reply

JP Jagadeesan Pichaimuthu Syncfusion Team March 27, 2019 12:52 PM UTC

Hi Julius, 

Thanks for using Syncfusion products. 

We have analyzed your reported scenario at our end. In order to set the Horizontal alignment of the button column, You can directly set the CellStyle property of the column and remove it from the QueryButtonCellStyle event. Refer to the following code snippet, 

Code Sample: 
//Set the Horizontal Alignment of the column in constructor or form load. 
this.sfDataGrid1.Columns["Open"].CellStyle.HorizontalAlignment = HorizontalAlignment.Center; 
this.sfDataGrid1.Columns["Map"].CellStyle.HorizontalAlignment = HorizontalAlignment.Center; 
 
void sfDataGrid1_QueryButtonCellStyle(object sender, QueryButtonCellStyleEventArgs e) 
    //Finds the column with 
    if (e.Column.MappingName.Equals("Open")) 
    { 
        //if the project exists then have an open icon else have a different icon 
        if (Directory.Exists(projectFolderPath)) 
        { 
            e.Button.Image = Properties.Resources.open.ToBitmap(); 
            e.Button.ImageSize = new System.Drawing.Size(w, h); 
        } 
        else 
        { 
            e.Button.Image = Properties.Resources.failedopen.ToBitmap(); 
            e.Button.ImageSize = new System.Drawing.Size(w, h); 
        } 
    } 
    //Another button with another icon 
    if (e.Column.MappingName.Equals("Map")) 
    { 
        var w = Properties.Resources.edit.Width / 4; 
        var h = Properties.Resources.edit.Height / 4; 
        e.Button.Image = Properties.Resources.map.ToBitmap(); 
        e.Button.ImageSize = new System.Drawing.Size(w, h); 
    } 
 
 
Let us know whether this helps also if you need any further assistance on this. 

Regards, 
Jagadeesan

Loader.
Live Chat Icon For mobile
Up arrow icon