Hi,
I am Developing a Windows Application in VB.NET. In Which one of the
Windows Form will Display data in DataGridView Control. I Need a Checked
DropDownList Column in DataGridView.
For this process i have Created a Custom Control (Checked DropDownList
Control), But if i add that control in to the
DataGridview Control i am unable to access that control in Datagridview both
inGUI and Code.
The Following are code, please point out the problem that i have done..
/ *------------------------------------------------------ Code Starts
Here
----------------------------------------------------------------------------------------------
* /
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
namespace dgdCheckedListBox
{
public class CheckedListBoxColumn : DataGridViewColumn
{
public CheckedListBoxColumn()
: base(new CheckedListBoxCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CheckedListBoxCell)))
{
throw new InvalidCastException("Must be a CheckedListBoxCell");
}
base.CellTemplate = value;
}
}
}
public class CheckedListBoxCell : DataGridViewCell
{
public CheckedListBoxCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CheckedListBoxEditingControl ctl =
DataGridView.EditingControl as CheckedListBoxEditingControl;
InitializeCheckedListBox(ctl, (ICollection)this.FormattedValue);
}
private void InitializeCheckedListBox(CheckedListBox ctrl, ICollectionvalue)
{
ctrl.Items.Clear();
foreach (object obj in value)
{
ctrl.Items.Add(obj.ToString());
}
ctrl.Tag = this.Value;
}
public override Type EditType
{
get
{
return typeof(CheckedListBoxEditingControl);
}
}
protected override object GetFormattedValue(object value, int rowIndex, ref
DataGridViewCellStyle cellStyle,
System.ComponentModel.TypeConvertervalueTypeConverter,
System.ComponentModel.
TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContextscontext)
{
if (value == null)
{
return new List<object>();
}
return base.GetFormattedValue(value, rowIndex, ref cellStyle,
valueTypeConverter, formattedValueTypeConverter, context);
}
public override Type FormattedValueType
{
get
{
return typeof(ICollection);
}
}
public override Type ValueType
{
get
{
return typeof(ICollection);
}
}
public CheckedListBox internalControl;
//private ComboBox lst = new ComboBox();
private CheckComboBoxTest.CheckedComboBox chkdrop = new CheckComboBoxTest.
CheckedComboBox();
protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState, object value,
objectformattedValue,
string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintPartspaintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
if (internalControl == null)
{
internalControl = new CheckedListBox();
}
internalControl.Items.Clear();
chkdrop.Items.Clear();
ICollection collection = value as ICollection;
//if (collection != null)
{
//foreach (object obj in collection)
{
internalControl.Items.Add("obj",true );
internalControl.Items.Add("obj1");
chkdrop.Items.Add("hi",true);
chkdrop.Items.Add("hi1",true);
//chkdrop.DroppedDown = true;
//lst.Items.Add("Hisdfsdf");
//lst.Items.Add("ghjgjhg");
}
{
}
Bitmap bmp = new Bitmap(cellBounds.Width, cellBounds.Height);
//internalControl.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width,
bmp.Height));
chkdrop.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
//lst.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
graphics.DrawImage(bmp, cellBounds, new Rectangle(0, 0, bmp.Width,
bmp.Height), GraphicsUnit.Pixel);
}
}
protected override void OnClick(DataGridViewCellEventArgs e)
{
//base.DataGridView.BeginEdit(false);
//base.OnClick(e);
}
}
class CheckedListBoxEditingControl : CheckedListBox,
IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CheckedListBoxEditingControl()
{
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Tag;
}
set
{
// this.Tag = value;
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
}
}
/ *------------------------------------------------------ Code Ends
Here
----------------------------------------------------------------------------------------------
* /
Thanks
Regards
Aravindh.K