Welcome to MSDN Blogs Sign in | Join | Help

Alex Yakhnin's Blog

Development and architecture for Windows Mobile devices. And a few useful tips on the way...

News

DataGrid with a custom header and selection.

One of the questions that has recently come up on the .Net CF MSDN forums was on how to create a custom (owner drawn) header in the DataGrid. This person wanted to implement the look similar to the grid that is used in the Mobile CRM application that has a non standard header and row selection.

crm_mobile

Both are using a gradient filled colors. So I decided to take this up for a challenge to create a similar look by using the standard DataGrid control that comes with .NET CF v2 SP2. Ilya Tumanov from the .NET CF team had create a great sample that shows how to create a custom column types and to override the Paint event in the DataGridTextBoxColumn. In my sample I've reused some code from his sample.

As a first step I have created the CustomSelectionColumn class that inherits from the DataGridTextBoxColumn. Here's how the Paint method looks:

 protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, 
                   int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
 {
     RectangleF textBounds;      // Bounds of text 
     object cellData;     // Object to show in the cell 

     // Check if the row is selected
     if (gridControl.IsSelected(rowNum))
     {               
         DrawBackground(g, bounds, rowNum, backBrush, true); 
     }
     else
     {
         DrawBackground(g, bounds, rowNum, backBrush, false);
     }
            
      // Draw cell background
      bounds.Inflate(-2, -2);    // Shrink cell by couple pixels for text.

      textBounds = new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height);
      // Set text bounds.
      cellData = this.PropertyDescriptor.GetValue(source.List[rowNum]); // Get data for this cell from data source.

      g.DrawString(FormatText(cellData), this.gridControl.Font, foreBrush, textBounds, this.StringFormat);                       
  }

In order to draw a gradient filled selector in the grid, I went to the MSDN library samples for .NET CF and copied the code from the How to: Display a Gradient Fill article into my project. So the DrawBackround method utilizes the code from this article:

 

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush, bool selected)
{
    // Create rectangle for drawing       
    Rectangle rc = new Rectangle(bounds.Left - 2, bounds.Top, bounds.Width + 4, bounds.Height);
    // Check if the row is selected
    if (selected)
    {
        // Draw gradient cell selection
        GradientFill.Fill(g, rc, Color.LightBlue, Color.RoyalBlue, GradientFill.FillDirection.TopToBottom);
    }
    else
    {
        g.FillRectangle(backBrush, rc);
    }            
}

 

So this should take care of a custom row selection. Now up to the custom header. A few years ago I had created an owner-drawn header control for a ListView. I have modified the CustomHeader control to use with the DataGrid and added the gradient drawing code to it. Here is the how the result looks:

custom_grid

You can download the full demo code from here.

Posted: Sunday, November 11, 2007 12:59 AM by priozersk

Comments

bnaya said:

the link for the source is missing

# November 11, 2007 8:18 AM

{ The Smoking Code } said:

DataGrid with a custom header and selection

# November 13, 2007 3:45 PM

Mighell's Mobile Blog said:

Chi, guardando qualche affascinente demo, non è rimasto colpito dalla grafica usata nelle applicazioni

# November 13, 2007 4:33 PM

cf_joe said:

Alex,

I know this request might be kinda of far but I using your code for displaying images on the header of Listview(http://blog.opennetcf.org/ayakhnin/CategoryView,category,ListView%20header.aspx ) but I also need to display images on the subitems (2 or 3 col) I am don't know the correct sendmessage to enable the Listview to get an handle to ListView and also set the image on certain columns of Subitem ListView. Can you kindly point me in the right direction.

Thanks,

# April 4, 2008 12:12 PM

selmaguzel said:

Hi,

I tried your code for displaying images on the listview(http://blog.opennetcf.com/ayakhnin/CategoryView,category,ListView%20header.aspx).

But when i try to this for datagrid, i got error as there is not a handler for datagrid.I changed the same code for datagrid only.

Then,

how can i display image on the datagrid column header?

Is it possible?

Thanks.

# August 27, 2008 5:46 AM

ghb9812052 said:

Hi

the link for demo code is missing.

Can you send me ? hbguo@wind.com.cn

thanks!

# October 5, 2008 10:30 PM
Anonymous comments are disabled
Page view tracker