Wednesday, June 3, 2015

Make rows expandable in a DevExpress ASPxGridView to show further goodies.

  1. at the code behind this...
    Foo.DataSource = whatever;
    Foo.DataBind();

    ...needs to be changed up like so...
    Foo.DataSource = whatever;
    Foo.KeyFieldName = "KeyID";
    Foo.DataBind();
     
  2. at the web form this...
       </Columns>
    </dx:ASPxGridView>

    ...needs to be changed up like so...
       </Columns>
       <Templates>
          <DetailRow>
             <div>Whatever</div>
          </DetailRow>
       </Templates>
       <SettingsDetail IsDetailGrid="True" ShowDetailRow="True"
             AllowOnlyOneMasterRowExpanded="false" />
    </dx:ASPxGridView>
     

Alright, well, what if you want to put another ASPxGridView nested in your ASPxGridView where the word "Whatever" is above? First, you'd need to wire up an event for expanding the rows to reveal the content like so:

Foo.DetailRowExpandedChanged += Foo_DetailRowExpandedChanged;

 
 

Assuming before that MyChildGrid is the name of your child grid and that FirstId and SecondId are int values kept in columns in Foo (the parent grid) that you will need to query data for each row in Foo for populating MyChildGrid, you might do this:

void Foo_DetailRowExpandedChanged(object sender,
      ASPxGridViewDetailRowEventArgs e)
{
   if (e.Expanded)
   {
      object[] keys = (object[])Foo.GetRowValues(e.VisibleIndex, new string[] { "FirstId",
            "SecondId" });
      ASPxGridView child = Foo.FindDetailRowTemplateControl(e.VisibleIndex,
            "MyChildGrid") as ASPxGridView;
      child.DataSource = _whatever.GetWhatever(keys[0], keys[1]);
      child.DataBind();
   }
}

 
 

Addendum 12/7/2015: This will make it so only one detail row may be expanded at once.

MyGrid.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;

No comments:

Post a Comment