It is not very difficuilt to do what you are asking. To have a column host
you user control, you need to implement your ount custom datagridview column
and you own datagridview cell.
Example :
this would be your daragridview column
public class DataGridViewUsrCtrlColumn : DataGridViewColumn
{
public DataGridViewUsrCtrlColumn() : base(new DataGridViewUsrCtrlCell())
{
}
}
then you need to implement you own datagridview cell. Example :
public class DataGridViewUsrCtrlCell : DataGridViewTextBoxCell
{
private readonly TextFormatFlags mCellFlags =
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
private readonly Font mFont = new Font("Time New Roman", 10);
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
YourUsrCtrl ctrl = DataGridView.EditingControl as YourUsrCtrl;
if (ctrl != null)
{
ctrl.MyValue = (YourValue)this.Value;
// Initialise the control with the object
}
}
public override Type EditType
{
get
{
// this is the type of uour control that will be hostend when cell is in
edit mode
return typeof(YourUsrCtrl);
}
}
public override Type ValueType
{
get
{
// this is the type of your object hosted on the cell
return typeof(YourObject);
}
}
public override object ParseFormattedValue(object formattedValue,
DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter
formattedValueTypeConverter, System.ComponentModel.TypeConverter
valueTypeConverter)
{
// return the right object that is parsed from the text
return this.Value;
}
protected override object GetFormattedValue(object value, int rowIndex, ref
DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter
valueTypeConverter, System.ComponentModel.TypeConverter
formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
return value.ToString();
// Put code to format the value correctly
}
protected override void Paint(Graphics graphics, Rectangle clipBounds,
Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText, DataGridViewCellStyle
cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (value != null)
{
string fmtValue = (string)formattedValue;
Size proposedSize = new Size(cellBounds.Width, cellBounds.Height);
graphics.FillRectangle(Brushes.Blue, cellBounds);
TextRenderer.DrawText(graphics, "My text", mFont, cellBounds, Color.White,
mCellFlags);
}
}
}
hope this helps
Fitim Skenderi
> Hello,
>
[quoted text clipped - 37 lines]
>> User Control. So, I don't see why you can't do the same thing on a
>> Datagrid control in cell.
Dan Erc. - 26 Nov 2007 19:42 GMT
Thanks for the sample code.
From what I understand this code causes the user control to be opened only
in editing mode. What I meant is that it will always have the appearance of
the user control. for example: If my user control contains 2 buttons and a
check box , the cell will always display the 2 buttons and the check box
(within the single cell).
As far as I know when we talk about web pages there is no problem to put any
web user controls within a table cell but when it comes to Windows forms, it
is a little bit complex.
Am I right?
Thanks
Eric
> It is not very difficuilt to do what you are asking. To have a column host
> you user control, you need to implement your ount custom datagridview
[quoted text clipped - 186 lines]
>>> User Control. So, I don't see why you can't do the same thing on a
>>> Datagrid control in cell.
Fitim Skenderi - 27 Nov 2007 09:34 GMT
In that case you need to inherit from DataGridViewCell then override the
paint method where you would draw you buttons, since in a sense even when
you have a button control on the datagridview the button is only painted,
and only when you click on the button (on the cell) the cell goes into
editing mode and it hosts the button(s).
Fitim Skenderi
> Thanks for the sample code.
>
[quoted text clipped - 203 lines]
>>>> with the User Control. So, I don't see why you can't do the same thing
>>>> on a Datagrid control in cell.
Eric Dan - 27 Nov 2007 16:19 GMT
Thanks for your help

Signature
Eric
> In that case you need to inherit from DataGridViewCell then override the
> paint method where you would draw you buttons, since in a sense even when
[quoted text clipped - 211 lines]
> >>>> with the User Control. So, I don't see why you can't do the same thing
> >>>> on a Datagrid control in cell.