search
Twitter Rss Feeds
MicrosoftArticlesForumsGroups
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml/Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsGroups
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsGroups
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsGroups
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsGroups
Microsoft Excel
Microsoft Word
Microsoft Powerpoint
Publisher
Money

Operating SystemsArticlesForumsGroups
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsGroups
Share Point
BizTalk
Site Server
Exhange Server
IIS
Transaction Server

Graphic DesignArticlesForumsGroups
Macromedia Flash
Adobe PhotoShop
Microsoft Expression

OtherArticlesForumsGroups
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Reviews
Search Engines
Resumes

 

Winforms DataGrid ComboBox Columns
And Custom Row Coloring

by Peter A. Bromberg, Ph.D.

Peter Bromberg

"Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy."
-- Groucho Marx

The Windows Forms DataGrid is a complex control with lots of features. However, unlocking its full potential is not trivial. This article covers a kind of "two in one" approach to provide two of the most common requests developers want in extending the Windows Forms DataGrid -- having a ComboBox (Dropdown) in a cell, and providing custom coloring of rows based on the textual contents of a given column.

Whenever I need to override a property or member of a Windows Forms control, I've learned that often the first place to look is George Shepherd's Windows Forms FAQ site. Shepherd is a contributing editor for MSDN Magazine, an instructor with DevelopMentor, and the author of a number of programming books. As a research scientist with Rockwell Scientific, he develops large software systems for a wide range of clients, and is also a contributing architect for Syncfusion's tools for .NET. George is an expert at Windows Forms and also at ASP.NET.

This article modifies and combines two "tricks" presented on George's FAQ site. Whenever you want a cell in a DataGrid to "do something different" you will usually need to override a base method on the DataGrid class, or create a custom DataGridTextBoxColumn - derived class. Such is the case here. In order to replace the contents of a cell with a ComboBox, we need a "DataGridComboBoxColumn". Here is some sample code I've modified from Shepherd's original sample to include the overridden Paint method that also allows us to color the cell:

public class DataGridComboBoxColumn : DataGridTextBoxColumn

{

public NoKeyUpCombo ColumnComboBox;

private CurrencyManager _source;

private int _rowNum;

private bool _isEditing;

public static int _RowCount;

private DataGrid dgForms;

private DataTable xdt;

private string _columnName;

 

public DataGridComboBoxColumn() : base()

{

_source = null;

_isEditing = false;

_RowCount = -1;

 

ColumnComboBox = new NoKeyUpCombo();

ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

 

ColumnComboBox.Leave += new EventHandler(LeaveComboBox);

// ColumnComboBox.Enter += new EventHandler(ComboMadeCurrent);

ColumnComboBox.SelectionChangeCommitted += new EventHandler(ComboStartEditing);

}

 

public DataGridComboBoxColumn(DataGrid dgForms, DataTable xdt, string columnName) : base()

{

_source = null;

_isEditing = false;

_RowCount = -1;

_columnName =columnName;

ColumnComboBox = new NoKeyUpCombo();

ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

ColumnComboBox.Leave += new EventHandler(LeaveComboBox);

// ColumnComboBox.Enter += new EventHandler(ComboMadeCurrent);

ColumnComboBox.SelectionChangeCommitted += new EventHandler(ComboStartEditing);

this.dgForms = dgForms;

this.xdt = xdt;

}

 

private void ComboStartEditing(object sender, EventArgs e)

{

_isEditing = true;

base.ColumnStartedEditing((Control) sender);

}

 

private void HandleScroll(object sender, EventArgs e)

{

if (ColumnComboBox.Visible)

ColumnComboBox.Hide();

 

}

 

// private void ComboMadeCurrent(object sender, EventArgs e)

// {

// _isEditing = true;

// }

//

private void LeaveComboBox(object sender, EventArgs e)

{

if (_isEditing)

{

SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);

_isEditing = false;

Invalidate();

}

ColumnComboBox.Hide();

this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler(HandleScroll);

 

}

 

protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)

{

base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

 

_rowNum = rowNum;

_source = source;

 

ColumnComboBox.Parent = this.TextBox.Parent;

ColumnComboBox.Location = this.TextBox.Location;

ColumnComboBox.Size = new Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);

ColumnComboBox.SelectedIndex = ColumnComboBox.FindStringExact(this.TextBox.Text);

ColumnComboBox.Text = this.TextBox.Text;

this.TextBox.Visible = false;

ColumnComboBox.Visible = true;

this.DataGridTableStyle.DataGrid.Scroll += new EventHandler(HandleScroll);

 

ColumnComboBox.BringToFront();

ColumnComboBox.Focus();

}

 

protected override void Paint(Graphics g, Rectangle bounds,

CurrencyManager source, int rowNum,

Brush backBrush, Brush foreBrush, bool alignToRight)

{

string sex=String.Empty ;

if (xdt != null)

{

if (xdt.Rows.Count > 0 && rowNum <= xdt.Rows.Count)

{

if (xdt.Rows[rowNum] != null)

{

try

{

sex = (string) xdt.Rows[rowNum][_columnName];

}

catch

{

// swallow invalid cast exceptions (not good practice, but works)

}

if (sex != null)

{

if (sex == "Owner")

backBrush = Brushes.Red;

else if (sex == "Sales Representative")

backBrush = Brushes.Yellow;

else if (sex == "Accounting Manager")

backBrush = Brushes.LightBlue;

else

backBrush = Brushes.White;

}

base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);

}

}

 

}

}

 

protected override bool Commit(CurrencyManager dataSource, int rowNum)

{

if (_isEditing)

{

_isEditing = false;

SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);

}

return true;

}

 

protected override void ConcedeFocus()

{

Debug.WriteLine("ConcedeFocus");

base.ConcedeFocus();

}

 

protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum)

{

object s = base.GetColumnValueAtRow(source, rowNum);

DataView dv = (DataView) ((DataTable) this.ColumnComboBox.DataSource).DefaultView;

int rowCount = dv.Count;

int i = 0;

//if things are slow, you could order your dataview

//& use binary search instead of this linear one

while (i < rowCount)

{

if (s.Equals(dv[i][this.ColumnComboBox.ValueMember]))

break;

++i;

}

 

if (i < rowCount)

return dv[i][this.ColumnComboBox.DisplayMember];

 

return DBNull.Value;

}

 

protected override void SetColumnValueAtRow(CurrencyManager source, int rowNum, object value)

{

object s = value;

 

DataView dv = (DataView) ((DataTable) this.ColumnComboBox.DataSource).DefaultView;

int rowCount = dv.Count;

int i = 0;

 

//if things are slow, you could order your dataview

//& use binary search instead of this linear one

while (i < rowCount)

{

if (s.Equals(dv[i][this.ColumnComboBox.DisplayMember]))

break;

++i;

}

if (i < rowCount)

s = dv[i][this.ColumnComboBox.ValueMember];

 

else

s = DBNull.Value;

base.SetColumnValueAtRow(source, rowNum, s);

}

}

 

public class NoKeyUpCombo : ComboBox

{

private const int WM_KEYUP = 0x101;

 

protected override void WndProc(ref Message m)

{

if (m.Msg == WM_KEYUP)

{

//ignore keyup to avoid problem with tabbing & dropdownlist;

return;

}

base.WndProc(ref m);

}

}

I also have a second class called DataGridColorTextBoxColumn that only implements the overridden Paint method, in order to also be able to colorize regular "non-combo" cells. The way this is implemented is that we will get a DataSet containing a DataTable of some columns from the Northwind Customers Table, and we will also have a second table of distinct ContactTitle items which will be used in the custom ComboBox Column, to populate the dropdown controls. This is all put together with the following DataBinding code:

private DataTable dtTitles;

private DataTable dtCustomers;

 

private void BindData()

{

string connString = ConfigurationSettings.AppSettings["connectionString"];

 

SqlDataAdapter da = new SqlDataAdapter("select CustomerID, ContactName, ContactTitle from customers;select distinct ContactTitle from customers", connString);

DataSet ds = new DataSet();

da.Fill(ds);

dtTitles = ds.Tables[1];

dtTitles.TableName = "ContactTitle";

dtCustomers = ds.Tables[0];

dtCustomers.TableName = "Customers";

 

DataGridTableStyle tableStyle = new DataGridTableStyle();

tableStyle.MappingName = "Customers";

DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(this.dataGrid1, dtCustomers, "ContactTitle");

for (int i = 0; i < dtCustomers.Columns.Count; ++i) {

if (dtCustomers.Columns[i].ColumnName != "ContactTitle")

{

DataGridColorTextBoxColumn TextCol = new DataGridColorTextBoxColumn(dataGrid1,dtCustomers,"ContactTitle");

TextCol.MappingName = dtCustomers.Columns[i].ColumnName;

TextCol.HeaderText = dtCustomers.Columns[i].ColumnName;

TextCol.Width = 175;

tableStyle.GridColumnStyles.Add(TextCol);

}

else

{

ComboTextCol.MappingName = "ContactTitle"; //must be from the grid table...

ComboTextCol.HeaderText = "ContactTitle";

}

}

ComboTextCol.Width = 200;

ComboTextCol.ColumnComboBox.DataSource = dtTitles;

ComboTextCol.ColumnComboBox.DisplayMember = "ContactTitle"; //use for display value in combo

ComboTextCol.ColumnComboBox.ValueMember = "ContactTitle"; // also use for value member in combo

tableStyle.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 2;

tableStyle.GridColumnStyles.Add(ComboTextCol);

dataGrid1.TableStyles.Clear();

dataGrid1.TableStyles.Add(tableStyle);

dataGrid1.DataSource = dtCustomers;

}

And the result looks like so:

CustomDataGrid showing dropdown (ComboBox) column and context-based row coloring in action.

The code as presented here will automatically set the selected value in each DataGridComboBoxColumn to the item from the DataRow that comes out of your database. You can also change a selected Item and call an Update with a DataAdapter, and the new value will be updated to your database. Of course, you'll need the proper Select, Update, Insert and Delete Commands to match.

You can download the VS.NET 2003 Solution zip file with all sample code here. Be sure to adjust the connection string in the config file to match your environment. Enjoy!


Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform. Pete's samples at GotDotNet.com have been downloaded over 41,000 times. You can read Peter's UnBlog Here.  --><--NOTE: Post QUESTIONS on FORUMS!
Article Discussion:


Pete's Blog   |    Pete's Resume   |    Robbe's Blog   |    Robbe's Resume   |    Archive #2   |    Archive #3   |    Dotnetslackers   |    XmlPitStop   |    Advertise   |   Contact Us   |   Privacy   |   Copyright (c) 2000 - 2009 eggheadcafe.com  All rights reserved.