Saturday, July 02, 2005

Row Headers in infragistics UltraGrid control

A problem was puzzling me with the infragistics UltraGrid control the other day, how to get row headers in the grid?



My first thought was "well the UltraGrid is such a huge control, there must be some property somewhere I can set to do this", something like.... myGrid.DisplayLayout.Rows[i].Header = "Row Name" or myGrid.DisplayLayout.Override.RowSelectors[i].Text = "Row Name" etc.... I spent ages looking all to no avail.

After all else failed, I resorted (as a last ditch attempt) to the infragistics knowledge base where I found this article.

Now this just talks about getting rid of the pencil, but slightly adapt it and you get the following


class RowSelectorDrawFilter : IUIElementDrawFilter
{
public RowSelectorDrawFilter(List<string> rowNames)
{
myRowNames = rowNames;
}

public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)
{
// Drawing RowSelector Call DrawElement Before the Image is Drawn
if (drawParams.Element is Infragistics.Win.UltraWinGrid.RowSelectorUIElement)
return DrawPhase.BeforeDrawImage;
else
return DrawPhase.None;
}

public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
{
// If the image isn't drawn yet, and the UIElement is a RowSelector
if (drawPhase == DrawPhase.BeforeDrawImage && drawParams.Element is Infragistics.Win.UltraWinGrid.RowSelectorUIElement)
{
// Get a handle of the row that is being drawn
UltraGridRow row = (UltraGridRow)drawParams.Element.GetContext(typeof(UltraGridRow));

drawParams.DrawString(drawParams.Element.Rect, myRowNames[row.ListIndex], false, false);
return true;
}

// Else return false, to draw as normal
return false;
}



private List<string> myRowNames = null;
}


then add the following line to the OnLoad method


myTableGrid.DrawFilter = new RowSelectorDrawFilter(myRowNames);


and providing the row names list is in the same order as the underlying table, it all just works. It is important that row.ListIndex is used to index into the rowNames array instead of row.Index, as row.Index is the display index, and if the grid is sorted, the row names will not be sorted.



Hope this helps someone else out there.

8 comments:

  1. just saw your article while looking up help for rowheaders in infragistics ultragrid. Thank you. i think it is a great help. I just hope it works when i try it later today :)

    ReplyDelete
  2. Anonymous1:24 am

    thanks a lot..that really helps. I have been eating my brains trying to add rowheader to winform datagrid or ultragrid.

    ReplyDelete
  3. Anonymous11:40 pm

    Thank you for help. I have modified your code for displaying custom images for active row.

    ReplyDelete
  4. Anonymous12:20 am

    I had been searching for this, for a while. Your article was of great help, exactly what we needed. Thank You.

    ReplyDelete
  5. Anonymous1:15 am

    hi. it is the only source on the web in relation to the problem of customizing rowheader appearance. BUT! it doesn't work for me. the text is on the header but i can't increase the width of the header (only a part of the text is being shown). what is the problem?

    ReplyDelete
  6. Adrian1:47 am

    For those having problems with the selector width try
    public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
    {
    // If the image isn't drawn yet, and the UIElement is a RowSelector
    if (drawPhase == DrawPhase.BeforeDrawImage && drawParams.Element is RowSelectorUIElement)
    {
    // Get a handle of the row that is being drawn
    UltraGridRow row = (UltraGridRow)drawParams.Element.GetContext(typeof(UltraGridRow));
    row.Band.Override.RowSelectorWidth = 150; // required width for your text
    Rectangle rect = drawParams.Element.Rect;
    drawParams.DrawString(rect, _rowNames[row.ListIndex], false, false);

    return true;
    }

    // Else return false, to draw as normal
    return false;
    }

    ReplyDelete
  7. Anonymous3:41 am

    That's great but... I wanna put an image on the top-leftcorner header (the rowselector of the headers) ... how I can to do it?
    I cannot found any way to access to that zone of the ultragrid...

    Many thanks for all your help.

    ReplyDelete
  8. Charly3:26 am

    Question to Adrian : Isn't the boolean "Wrap text" useful in that case ?

    ReplyDelete