I have a EXGrid in a form and want to add button to a cell that either say "Edit" and "Remove", or Have icons for the two. Here is the code I have so far.

With grdGrade
    .BeginUpdate
        '.DataSource = objCo.DAL.Connection.Execute(sql)
        .DefaultItemHeight = 21

        .TreeColumnIndex = -1
        .MarkSearchColumn = False
        .FullRowSelect = exItemSel
        .DrawGridLines = exAllLines
        .SortOnClick = exNoSort
        .BackColorHeader = grdGrade.BackColor
        .Appearance = Flat
        .BackColorAlternate = Me.BackColor
        .SelBackColor = RGB(224, 224, 224)
        .SelForeColor = vbBlack
        .UseTabKey = True
        .Font = Me.Font

    .EndUpdate
End With

Set col = grdGrade.Columns.Add("Actions")
col.AllowDragging = False
col.Width = grdGrade.Width * 0.05
col.Editor.AddButton "Edit", , 0
col.Editor.AddButton "Remove", , 0
col.Editor.EditType = ButtonType

The buttons are there, they are just blank. Also, would it be possiable to show an icon as the button. Thanks so much for your help.

Dani AI

Generated

Nice find, — the per-cell button approach you used is a simple, reliable way to get visible text labels. If the goal is to show icons (or to keep small icon-only buttons in a column editor) there are a couple cleaner options in Exontrol's eXGrid/Editor API:

  • Use the column's Editor to show icon buttons. The Editor.AddButton call accepts an image (either an image-list index or a Picture), plus a tooltip/title; add your icons first with Editor.ReplaceIcon (or pass LoadPicture directly to AddButton). This is the intended way to put icons onto editor buttons. (exontrol.net)

  • Handle clicks centrally. The grid raises a ButtonClick event that receives the clicked button’s Key (the Key you gave to AddButton). Use that Key inside the grid’s ButtonClick handler to dispatch Edit vs Remove actions. (exontrol.com)

Small VB6 example (not copied from the thread) showing the pattern:

' add icons then buttons to the column editor
Dim iEdit As Long, iRemove As Long
iEdit = col.Editor.ReplaceIcon(LoadPicture("C:\icons\edit.ico").Handle)
iRemove = col.Editor.ReplaceIcon(LoadPicture("C:\icons\delete.ico").Handle)
col.Editor.AddButton "edit", iEdit, LeftAlignment, "Edit this row", "Edit"
col.Editor.AddButton "remove", iRemove, RightAlignment, "Remove this row", "Remove"

Then handle the grid event:

Private Sub grd_ButtonClick(Item As HITEM, ColIndex As Long, Key As Variant)
    Select Case Key
        Case "edit":  ' perform edit
        Case "remove": ' perform remove
    End Select
End Sub

If you need visible text on the button itself (not an icon), your split-cell + CellHasButton solution is perfectly fine. Another option is to host a full ActiveX button inside a cell using Items.InsertControlItem / ItemObject if you need richer visuals or per-cell event wiring. Also watch Editor.ImageSize and ButtonWidth so icons render crisply and not clipped. (exontrol.net)

Troubleshooting notes: icons must be added (ReplaceIcon) or passed as a Picture; an Image index of 0 means “no icon.” If a button looks blank, verify the image index, the editor’s ImageSize, and that the AddButton call happened after the icons were added. (exontrol.net)

I finally got it. When loading the grid I used

 Me.grdGrade.Items.CellValue(h, "Actions") = "    Edit    "
Me.grdGrade.Items.CellHasButton(h, "Actions") = True
s = Me.grdGrade.Items.SplitCell(h, "Actions")
Me.grdGrade.Items.CellValue(0, s) = "  Remove  "
Me.grdGrade.Items.CellHasButton(0, s) = True

and it works like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.