Saturday, May 17, 2014

Select the unique id of all selected rows in a DevExpress ASPxGridView and cast them to a List of int in C#.

I have an ASPxGridView with an opening tag like so wherein the KeyFieldName must match up to the name of a "unique id parameter" within the object of which the list of T that binds to the ASPxGridView is filled (databound) by.

<dx:ASPxGridView AutoGenerateColumns="False" ID="GridOfFoo"
      ClientIDMode="Static" runat="server" KeyFieldName="FooId">

 
 

In my ASPxGridView I have a GridViewCommandColumn full of checkboxes for selecting rows as detailed here and here. Far outside of the ASPxGridView I have a plain Jane button.

<dx:ASPxButton ID="Whatever" runat="server" AutoPostBack="false"
      EnableTheming="False" CssPostfix="none" CssClass="button" Text="Whatever">
      </dx:ASPxButton>

 
 

In the page loading stuff (web forms) I wire up this event for the button.

BetterButtonToBe.ClientSideEvents.Click =
      String.Format("function(s,e){{{0}.PerformCallback(JSON.stringify({1}));}}",
      myCallBackPanel.ClientInstanceName, json.Serialize(CertificationParams("Button",
      "MatchMeUpInCallBack")).Replace('\"', '\''));

 
 

I then get the "values" of the selected rows like this in the event for the callback panel:

void myCallBackPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
   var clientParams = json.Deserialize<FooClientParams>(e.Parameter);
   List<object> rawValues = GridOfFoo.GetSelectedFieldValues("FooId");
   List<int> values = rawValues.Cast<int>().ToList();

 
 

I don't really understand the line of code with the object deserialization above yet, and I could have left it out as it really does nothing. Some of this comes from an example at work. In that example we would match "MatchMeUpInCallBack" conditionally on something hanging off of clientParams to determine the intent of this particular callback. Ugh, I should have thought this out more before I typed it up. I am trying to not have caffeine on the weekends and it does make me bouncy during the work week, but, well, you know...

No comments:

Post a Comment