XtraGrid Suite - is there any way to add a button or a hyperlink to a cell?

I am working with the XtraGrid Suite created by DevExpress. I cannot find any functions for this, but I am curious if you can add a button or a hyperlink to the grid cell.

Context: I have a list of events. Each event has a time, start / end and category (utility and maintenance). There may be Start and Stop events. After analyzing the problem, I decided that StartTime and EndTime for each event would not work.

So, if the event starts, I record the current time on the Event object and set it as the "Start" event. I would like to add a stop button / hyperlink to a cell in this row. If the user wants to register the Ends event, type of event, etc. It will be copied to a new event with the type Stop and the button will disappear.

Hope this makes sense.

EDIT: The answer to Aaronaught is actually better than what I originally requested (button), so I updated the question. That way, anyone looking for a hyperlink in a cell can benefit from their example :)

+6
winforms devexpress xtragrid
source share
4 answers

I prefer to use hyperlink-style text, overriding the drawing code and handling mouse / mouse click events, since the buttons do not scale too well to the typical grid line height. However, if the button is really what you want, you should do it using RepositoryItemButtonEdit as the type of editor.

If you are interested in the first, leave a comment, and I will update it with an example. Otherwise, as already mentioned, just use RepositoryItemButtonEdit . You can change its properties to take the whole cell if you want, and then make the column a fixed size so that the button does not stretch.


UPDATE: I am posting some sample code for the “pretty hyperlink” below, which I like a lot better than the standard hyperlink cell, because (a) it looks better, (b) it gives feedback with the hang, and (c) you can change the cursor if you want (I use the utility method to get my own mouse cursor from the OS, which has a more three-dimensional appearance than the one built into Winforms).

Note for non-DevExpress users who read this: I use an almost identical method for the standard System.Windows.Forms.ListView . Microsoft uses this user interface pattern quite a bit in Vista and Windows 7, and it's good to learn how to do it, even if the result is not a perfect replica.

 private int hoverRowHandle = GridControl.InvalidRowHandle; private void gridView_Click(object sender, EventArgs e) { if (hoverRowHandle != GridControl.InvalidRowHandle) { MyItem item = gridView.GetRow(hoverRowHandle) as MyItem; if (item != null) // Do whatever the "click" action is here } } private void gridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) { if (e.Column == linkColumn) { bool hover = (hoverRowHandle == e.RowHandle); FontStyle style = hover ? FontStyle.Underline : FontStyle.Regular; TextFormatFlags formatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.WordEllipsis; Color foreColor = gridView.IsRowSelected(e.RowHandle) ? Color.White : (hover ? MyColors.LinkHover : MyColors.Link); using (Font font = new Font(gridControl.Font, style)) { TextRenderer.DrawText(e.Graphics, "Link Text", font, e.Bounds, foreColor, formatFlags); } e.Handled = true; } } private void gridView_MouseLeave(object sender, EventArgs e) { int tempRowHandle = hoverRowHandle; hoverRowHandle = GridControl.InvalidRowHandle; if (tempRowHandle != GridControl.InvalidRowHandle) { gridView.InvalidateRowCell(tempRowHandle, linkColumn); } } private void gridView_MouseMove(object sender, MouseEventArgs e) { int tempRowHandle = hoverRowHandle; if (tempRowHandle != GridControl.InvalidRowHandle) { hoverRowHandle = GridControl.InvalidRowHandle; gridView.InvalidateRowCell(tempRowHandle, linkColumn); } GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location); if (hitInfo.InRowCell && (hitInfo.Column == linkColumn)) { hoverRowHandle = hitInfo.RowHandle; gridView.InvalidateRowCell(hoverRowHandle, linkColumn); } bool hoverDetail = (hoverRowHandle != GridControl.InvalidRowHandle); gridControl.Cursor = hoverDetail ? Cursors.Hand : Cursors.Default; } 

A few comments about this code:

  • MyItem is any type of data that you have attached to a grid view. Maybe it's a DataRow , or maybe some kind of user-defined type if the data source is IList<T> .

  • MyColors is a utility class that defines a pair of public static readonly Color fields used for user interface content. You can replace links to this with hard-coded colors if you are only going to do it on the same grid.

  • I am not trying to cache Font , although you probably could, since there are only two of them.

  • The cursor logic will work with any other cursor logic that you can use in the grid (for me, there are almost no cases, therefore, as a rule, everything should be fine).

  • If you want to have more than one link column, you need to save the hoverColumn status hoverColumn in addition to hoverRowHandle and obviously change these comparisons with a single column to search for multiple columns.

For my own Winforms applications, I actually have an Extender provider that allows me to attach this behavior to a GridView or ListView by flipping the column name / text to the column list, but this code is just a tiny bit too long to post here. In the above example you should start.

+15
source share

Use RepositoryItemButtonEdit and set the TextEditStyle parameter to HideTextEditor .

+5
source share

you can use RepositoryItemButtonEdit: select the target column, in the "Properties" click "ColumnEdit" and select "new", then select "ButtonEdit". click on your grid and select "run designer", select "Repository-repository in place" in the "Repository" group. select "repositoryItemButtonEdit1" (if you have not changed the name of the button editing component), select the "event" tab and select the "ButtonPressed" event. fill in this code in this case. if you want, hide the component editor, select the target column, in the "Properties" click "ColumnEdit", find "TextEditStyle" and select "HideTextEditor".

But, one question! ?? I want to add a picture to my button, does anyone have an idea?

+1
source share

This can now be achieved using the RepositoryItemHyperLinkEdit control.

See: RepositoryItemHyperLinkEdit Class

+1
source share

All Articles