9

Using DevExpress's GridView, I would like to trigger a (clientside) event when a cell is selected (or simply clicked on).

There already is a way to get the click events for an entire row, but neither fiddling around nor the documentation gives me any clue how to achieve this for cells.

This is what I have for rows:

Html.DevExpress().GridView(settings =>
{
    // removed a lot of code here
    settings.ClientSideEvents.RowDblClick = "OnGridRowDblClick";
}).Bind(Model).GetHtml()

Which will cause the javascript function OnGridRowDblClick to be called when a row is double clicked. Ideally there should be something like

settings.ClientSideEvents.CellClick = "OnCellClick";

However, this does not exist, nor can I find anything to achieve this.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
René Wolferink
  • 3,558
  • 2
  • 29
  • 43

1 Answers1

13

It is possible to attach the required client-side handler for an individual DataCell by handling the GridViewSettings.HtmlDataCellPrepared event:

function OnCellClick(visibleIndex, fieldName) {
    alert(visibleIndex + " " + fieldName);
}


@Html.DevExpress().GridView(settings => {
    ...
    settings.HtmlDataCellPrepared = (sender, e) => {
        e.Cell.Attributes.Add(
            "onclick",
            string.Format("OnCellClick('{0}', '{1}');", e.VisibleIndex, e.DataColumn.FieldName)
        );
    };

}).Bind(Model).GetHtml()
Mikhail
  • 9,186
  • 4
  • 33
  • 49
  • You're a life-saver! Since the elements are all generated, I also didn't find a way of setting the onclick-events. Besides, this allows me to place it only on cells where it is appropriate because the handler also allows some logic to happen. Thanks! – René Wolferink Aug 08 '12 at 08:14
  • This doesn't work if `myGridView.StartEditRow()` is called on row click. How to get cell click event even after edit mode is entered? – Sнаđошƒаӽ May 16 '17 at 12:20