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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Button condition Styling in Datagrid

Thread ID:

Created:

Updated:

Platform:

Replies:

143561 Mar 26,2019 08:16 PM UTC Mar 27,2019 12:52 PM UTC WinForms 1
loading
Tags: SfDataGrid
Julius
Asked On March 26, 2019 08:18 PM UTC

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.

Jagadeesan Pichaimuthu [Syncfusion]
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon