Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / November 2007

Tip: Looking for answers? Try searching our database.

Hosting any user control in a DataGridView cell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Erc. - 24 Nov 2007 17:14 GMT
Hi,

I have seen the articles related to hosting a date-time picker control
within a DataGridView cell and how to implement a
DataGridViewRadioButtonCell and column.

My question is generic and simple (but probably the answer is not that
simple).

Should it be simple to host any user control by just understanding the 2
examples mentioned above?

Will it be simple to "delegate" the complex painting job to the user
control? and just implement and override the necessary methods in order to
be hosted correctly in a cell?

Is there some white paper related to hosting any user control within a
DataGridViewCell?

I am not expecting a fully detailed answer to my generic question :-)
(unless there is already a full specification for my question).

Thanks in advance
Mr. Arnold - 25 Nov 2007 02:52 GMT
> Hi,
>
[quoted text clipped - 19 lines]
>
> Thanks in advance

I am doing a project where a User Control that holds several controls is
being implemented repeatedly  on a scrollable Panel control with each being
used on the Panel control as a single instance of the User Control object,
with each User Control being able to instantiate other controls on the form
or the UI level in other forms  no not associated with the 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 04:23 GMT
Hello,

I have also managed to do exactly what you have described with a Panel
control hosting user controls, but when it comes to a DataGridView control
it becomes a totally different story. See the radio-button cell example
which invloves with quite a complex "painting" code even though I have
learned a great deal from it.

Thanks

>> Hi,
>>
[quoted text clipped - 27 lines]
> Control. So, I don't see why you can't do the same thing on a Datagrid
> control in cell.
Fitim Skenderi - 26 Nov 2007 14:55 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.