Click here to Skip to main content
Licence 
First Posted 29 Feb 2004
Views 142,792
Bookmarked 41 times

Extending the DataGrid

By | 19 May 2004 | Article
This article demonstrates how to reorder columns (using column drag-and-drop), how to create icon columns, and how to use DataGrid table styles.

Sample Image - ColumnReorderDataGrid.jpg

Introduction

Since DataGrid is a generic control, it doesn't provide column reordering. If you need this functionality, you have to implement drag and drop handling code yourself. Code itself is quite straight-forward. The reason I'm sharing it is to:

  1. prove that it is doable and easy
  2. save time for those who are not familiar with drag-and-drop (like myself when I started this mini-project).

C# doesn't provide any support for dragging a semi-transparent image during drag and drop (e.g., when you drag a column in Windows Explorer, you can see a column header image being dragged). I've used a code that I've found here and integrated it with my application.

I also demonstrate how to implement image columns. There are samples in MSDN for ComboBox and DateTimePicker column, but there was no image column. Again, it is very easy and code is included in this sample.

I've added support for a table style that auto-resizes last column in the grid, so that grid consumes all the space in the control. Some people reported issues with that but I've got no repro. Let me know if you hit any problem.

I've added support for a visual cues when mouse hovers over the column header and for a drop location during drag-n-drop. This is very cool. Certain capabilities were not exposed by the DataGrid (some important properties are private), so I had to define them second time (BorderWidth, HeaderHeight, HScrollPosition). Pay attention to the HScrollPosition property. I use the scroll bar to figure out the scroll position so that I can paint correctly drop location for drag-n-drop operation.

Finally, I've updated the code sample recently to include a feedback about the sample design sent to me by Nimrod Cohen. Thanks Nimrod.

Using the code

Code is small and simple, so you should be able to figure out what is it doing, without any troubles. When drag operation starts, it creates a DragColumn. Drop operation retrieves index of the dragged column from that DragColumn object. The reason I'm using my own object and not one of the defined DataFormats is that drag-n-drop should only work within DataGrid and dragged object should not be recognized by any other application.

Also, for dragging an image of the column header, I use GDI+ to create the bitmap and use unmanaged APIs to drag it.

I was requested to show all the features step by step instead of giving one big example. So I've split my code into 3 peices and now you can learn new features gradually. You'll find 3 examples in the zip files. Example 1 being the simplest and example 3 being the most advanced one.

Also,

  1. You may want to enter data in the DataGrid manually, after you start the demo app.
  2. In order to reorder columns, drag a column header and drop it somewhere else in the DataGrid.

Points of Interest

Table style for auto-resizing columns was a little tricky. When column is auto-resized (e.g., when user double clicks on a border between columns) there is no WidthChange event fired by the grid column style. So, I had to change width manually (Width++ and Width--) to make sure the event is fired.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vlad P.

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBug (no biggie) PinmemberC++hristoffer7:43 15 Jul '06  
GeneralDifferent image for each row Pinmemberruth1:42 14 Dec '05  
GeneralDifferent icon for each row Pinmemberruth1:41 14 Dec '05  
GeneralDrag Image Pinmemberalam_pune10:13 9 Dec '05  
GeneralRe: Drag Image PinmemberVlad P.11:39 10 Dec '05  
QuestionIs there way ...? PinmemberSzymonK20:59 11 Aug '05  
AnswerRe: Is there way ...? PinmemberSzymonK21:42 11 Aug '05  
GeneralRe: Is there way ...? Pinmemberjenny117723:34 10 Oct '05  
GeneralChanging Icon in the DataGridIconColumnStyle PinmemberMikels22:09 10 Feb '05  
GeneralRe: Changing Icon in the DataGridIconColumnStyle PinmemberVlad P.4:38 19 Feb '05  
GeneralColumn header height and wordwrap Pinmembercuriousw12:12 12 Jan '05  
GeneralMULTIPLE CONTROLS IN A CELL PinmemberPradeep K V0:42 27 Sep '04  
GeneralRe: MULTIPLE CONTROLS IN A CELL PinmemberVlad P.7:05 9 Oct '04  
GeneralRe: MULTIPLE CONTROLS IN A CELL Pinmemberjebish21:38 16 Apr '07  
GeneralDifferenr icon for each row PinsussBobok20:46 6 Jul '04  
GeneralSizing when a vertical scroll bar is present PinmemberNick Craven-Smith5:51 1 Jul '04  
GeneralRe: Sizing when a vertical scroll bar is present PinmemberVlad P.4:46 19 Feb '05  
GeneralDouble Click on Border PinmemberAztekman11:03 10 Jun '04  
GeneralDrag&Drop PinsussDennis v/d Stelt2:21 8 May '04  
GeneralRe: Drag&Drop PinmemberVlad P.4:18 12 May '04  
GeneralRe: Drag&Drop PinmemberDennis v/d Stelt7:26 12 May '04  
GeneralBeware of bug that causes exception + correction patch until vladimir issues an official correction PinmemberDaberElay0:37 2 May '04  
GeneralRe: Beware of bug that causes exception + correction patch until vladimir issues an official correction PinmemberVlad P.4:14 12 May '04  
GeneralAn easier wrapper class PinmemberDaberElay1:46 2 Apr '04  
GeneralRe: An easier wrapper class PinmemberDaberElay12:08 17 May '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 May 2004
Article Copyright 2004 by Vlad P.
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid