Skip to content

Instantly share code, notes, and snippets.

@sdanna
Created May 4, 2012 19:13
Show Gist options
  • Save sdanna/2597102 to your computer and use it in GitHub Desktop.
Save sdanna/2597102 to your computer and use it in GitHub Desktop.
Winforms DataGridView Column with a custom header (Combobox + NumericUpDown control in this instance)
public class ComboNumericHeaderCell : DataGridViewColumnHeaderCell
{
public readonly ComboBox _comboBox;
public readonly NumericUpDown _numericUpDown;
public ComboNumericHeaderCell()
{
_comboBox = new ComboBox();
_numericUpDown = new NumericUpDown();
_comboBox.Font = Control.DefaultFont;
_numericUpDown.Increment = 1;
_numericUpDown.Value = 10;
_numericUpDown.Size = new Size(40, 20);
_numericUpDown.Font = Control.DefaultFont;
_comboBox.MouseClick += new MouseEventHandler(MouseClick);
_numericUpDown.MouseClick += new MouseEventHandler(MouseClick);
// TODO - Wireup necessary events
}
private void MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("x:{0}, y:{1}, button:{2}", e.X, e.Y, e.Button);
OnClick(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));
}
public string ComboBoxValue
{
get { return _comboBox.SelectedValue.ToString(); }
set { _comboBox.SelectedText = value; }
}
public decimal NumbericUpDownValue
{
get { return _numericUpDown.Value; }
set { _numericUpDown.Value = value; }
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
_comboBox.Location = new Point(cellBounds.Left, cellBounds.Top+3);
_comboBox.Height = cellBounds.Height;
_comboBox.Width = cellBounds.Width - 41;
_numericUpDown.Location = new Point(_comboBox.Right + 1, cellBounds.Top+3);
_comboBox.Height = cellBounds.Height;
//_comboBox.Width = FixedWidth;
}
}
public class SpecialColumnHeader : DataGridViewTextBoxColumn
{
ComboNumericHeaderCell headerCell = new ComboNumericHeaderCell();
public SpecialColumnHeader()
{
//this.CellTemplate = new DataGridViewTextBoxCell();
this.HeaderCell = headerCell;
this.Width = 150;
}
protected override void OnDataGridViewChanged()
{
if (DataGridView != null)
{
this.DataGridView.Controls.Add(headerCell._comboBox);
this.DataGridView.Controls.Add(headerCell._numericUpDown);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment