Tuesday, September 8, 2015

get custom buttons working at a DevExpress ASPxGridView

Make the button in/at one of the columns like so in the ASP.NET markup:

<dx:GridViewCommandColumn Caption="Whatever" ButtonType="Link">
   <CustomButtons>
      <dx:GridViewCommandColumnCustomButton Text="Download">
      </dx:GridViewCommandColumnCustomButton>
   </CustomButtons>
</dx:GridViewCommandColumn>

 
 

Do you want to only show a link for some records?

private void ReportGrid_HtmlCommandCellPrepared(object sender,
      ASPxGridViewTableCommandCellEventArgs e)
{
   if (e.CommandColumn.Caption == "Whatever")
   {
      if (e.KeyValue != null)
      {
         if (String.IsNullOrEmpty(TryToFindDownloadPath(e.KeyValue)))
         {
            e.Cell.Text = "";
         }
      }
   }
}

 
 

This method is referenced above:

private string TryToFindDownloadPath(object fileId)
{
   int matchMe = (int)fileId;
   foreach (Foo foo in _data.Where(foo => foo.FileId == matchMe)) {return foo.Path;}
   return null;
}

 
 

What happens when the link is clicked?

protected void Download_Callback(object sender,
      ASPxGridViewCustomButtonCallbackEventArgs e)
{
   var foo = (Foo)ReportGrid.GetRow(e.VisibleIndex);
   var id = foo.UniqueId;
   MakeTheFileDownloadHappen(id);
}

 
 

Obviously, this magic requires that two events be wired up to your grid.

ReportGrid.CustomButtonCallback += Download_Callback;
ReportGrid.HtmlCommandCellPrepared += ReportGrid_HtmlCommandCellPrepared;

 
 

In the above _data is a collection of Foo which is bound to ReportGrid which is an ASPxGridView. Get it?

No comments:

Post a Comment