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

VB.NET : Button Image inside GridControl - Change Image based on adjoining cell value

Hi, I have this code which creates button in Cell with a image.

Private Sub GridControlCertificateManager_QueryCellInfo(sender As Object, e As GridQueryCellInfoEventArgs) Handles GridControlCertificateManager.QueryCellInfo
        Select Case e.ColIndex
            Case 1
                e.Style.BackColor = Color.Azure
                e.Style.ReadOnly = True
            Case 5
                If e.RowIndex <> 0 Then
                    e.Style.CellType = GridCellTypeName.Control
                    e.Style.Control = btnPasteSrNo

                    btnPasteSrNo.Name = "btnPasteSrNo"
                    btnPasteSrNo.FlatStyle = FlatStyle.Popup
                    btnPasteSrNo.TabStop = False
                    e.Style.CellTipText = "Click to Paste Serial Nos."

                    btnPasteSrNo.ImageList = m_clsImageList
                    btnPasteSrNo.ImageIndex = 1
                End If
            Case 6
                If e.RowIndex <> 0 Then
                    'Creates the list.
                    Dim items As New StringCollection
                    items.AddRange(New String() {"Pass", "Fail"})

                    'Sets the style properties.
                    e.Style.CellType = "ComboBox"
                    e.Style.ChoiceList = items
                    If e.Style.CellValue.trim = "" Then
                        e.Style.CellValue = "Pass"
                    End If

                    'True droplist - no editing.
                    e.Style.DropDownStyle = GridDropDownStyle.Exclusive
                End If
            Case 10

                If e.RowIndex <> 0 Then
                    e.Style.CellType = GridCellTypeName.Control
                    e.Style.Control = btnAddFile

                    btnAddFile.Name = "btnAddFile"
                    btnAddFile.FlatStyle = FlatStyle.Popup

                    btnAddFile.TabStop = False
                    btnAddFile.ImageList = m_clsImageList

                    e.Style.CellTipText = "Click to Add Certificates"
                    btnAddFile.Image = m_clsImageList.Images(0)

                End If
        End Select
    End Sub

Everything is ok. Check code in RED Color; When I click this button it opens a dialogue box and allows to select a file. Then the Selected file path appears in the adjoining cell i.e., 9th cell and 10th cell is the button. 
What I am trying to achieve is when the cell 9 is empty the button image should be the initial one (that is AddButtonIcon from ImageList index 0) and if some path is available in that cell then Image should be another one (that is RemoveFileIcon from ImageList index 2).

How do we achieve this it's easily done in VB control like below;

Private Sub dataGridViewInspectionReportCertificateDetails_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles dataGridViewInspectionReportCertificateDetails.CellFormatting
        'e.Value = Global.ICCS.My.Resources.Resources._20removeFile
        If e.RowIndex < 0 OrElse Not e.ColumnIndex = _
                   dataGridViewInspectionReportCertificateDetails.Columns("addNewUpdatedReport").Index Then Return

        Select Case Trim(dataGridViewInspectionReportCertificateDetails.Rows(e.RowIndex).Cells("replaceReportPath").Value)
            Case ""
                e.Value = Global.ICCS.My.Resources.Resources._20add_file
            Case Else
                e.Value = Global.ICCS.My.Resources.Resources._20removeFile
        End Select

    End Sub

6 Replies

AR Amal Raj U Syncfusion Team September 22, 2016 02:58 PM UTC

Hi Nilofer, 

Thanks for using Syncfusion products. 

We are able to understand the reported scenario. The reported scenario of changing ImageIndex of the button based on another cell value can be achieved using PrepareViewStyleInfo event. Please make use of the below code, 
 
Code Example 
//Event Subscription. 
this.gridControl1.PrepareViewStyleInfo += new GridPrepareViewStyleInfoEventHandler(gridControl1_PrepareViewStyleInfo); 
 
void gridControl1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) 
    if (e.ColIndex == 2 && e.RowIndex == 3) 
    { 
        if (String.IsNullOrEmpty(e.Style.CellValue.ToString())) 
        { 
            button1.ImageList = imageList; 
            button1.ImageIndex = 1; 
        } 
        else 
        { 
            button1.ImageList = imageList; 
            button1.ImageIndex = 0; 
        } 
    } 
 
Sample Link 
 
Regards, 
Amal Raj U. 



NI Nilofer September 22, 2016 03:37 PM UTC

Thanks Amal. This only work in case of single button on entire grid. But if you see my code. Every row is having a button which get created every time we create a new row.
In that case its not working. it changes all button images.
I just want to change the button image where there is a value in cell next to it. All blank should be having other image.

Best Regards,


AR Amal Raj U Syncfusion Team September 23, 2016 03:19 PM UTC

Hi Nilofer, 

Thanks for the update. 

We are able to understand your scenario with the provided information. If you are using a single button object for all cells in a column and change the image index for the button, then the image will be changed for all the cell buttons. So you must have different object for different buttons in the columns. 

We suggest you to use the built-in  PushButton cell type and draw the cell image for the PushButton with the specified condition to change the image. Please make use of the below code, 

Code Example 
//Event Subscription. 
this.gridControl1.DrawCellButton += new GridDrawCellButtonEventHandler(gridControl1_DrawCellButton); 

void gridControl1_DrawCellButton(object sender, GridDrawCellButtonEventArgs e) 
    if (e.Style.CellType == GridCellTypeName.PushButton) 
    { 
        if (string.IsNullOrEmpty(gridControl1[e.RowIndex, 2].CellValue.ToString())) 
        { 
            e.Graphics.DrawImage(SystemIcons.Error.ToBitmap(), e.Button.Bounds); 
            e.Style.CellAppearance = GridCellAppearance.Raised; 
            e.Cancel = true
        } 
        else 
        { 
            e.Graphics.DrawImage(SystemIcons.Warning.ToBitmap(), e.Button.Bounds); 
            e.Style.CellAppearance = GridCellAppearance.Raised; 
            e.Cancel = true
        } 
    } 

Sample Link 

Regards, 
Amal Raj U. 



NI Nilofer September 23, 2016 05:15 PM UTC

Ok.
Thanks for the support and providing alternative way out for the problem. Though it would be difficult and have to work around on many things to make it work in my case.
But, still I am moving towards something achievable I feel now. Thanks again,

Best Regards,


NI Nilofer September 26, 2016 04:42 AM UTC

Thanks Amal. Just dropped in to say the work-around you pointed out worked and things have sorted out.
Thanks Again,

Best Regards,


AR Amal Raj U Syncfusion Team September 26, 2016 04:46 AM UTC

Hi Nilofer, 

Thanks for the update. 

We are glad to know that the provided solution has resolved your query. Please let us know, if you have any other concerns. 

Regards, 
Amal Raj U. 


Loader.
Up arrow icon