Host Control in DataGridView

F

Frederik

Hi all,

Can someone tell me how I can host a LinkLabel in a DataGridView (the
existing DataGridViewLinkColumn does not provide what I need). Do I
need to derive from DataGridViewTextBoxCell or from DataGridViewCell?

Many thanks for any feedback/examples.
Frederik
 
F

Frederik

As an addition, I'll mention what I want to accomplish (maybe you
smart people know a better solution): I want a column in my
DataGridView that has cells containing text (so far that's easy). Part
of the text however should be marked in some way (like a hyper link)
and an event must be fired when clicked on that marked text. Multiple
'hyper links' should be possible within the same cell (that's why I
was thinking about hosting the LinkLabel control).
 
F

Frederik

Thanks for your reply Nicholas.

I had a quick try to customize the example you provided and use it for
my own program, but stumbled on the fact that this custom cell uses a
method 'InitializeEditingControl'. In my case I need to show (without
editing) the LinkLabel. I have a feeling I need to overwrite other
methods in my case. I'll keep trying somewhat, but do not hesitate to
throw some more examples ;-)

Kind regards,
Frederik


Frederik,

What you are doing should be possible with a custom control for a
DataGridView. Here is a link to a sample which hosts a MaskedTextBox:

http://msdn2.microsoft.com/en-us/library/ms180996.aspx

You should be able to adapt this to your needs.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Can someone tell me how I can host a LinkLabel in a DataGridView (the
existing DataGridViewLinkColumn does not provide what I need). Do I
need to derive from DataGridViewTextBoxCell or from DataGridViewCell?
Many thanks for any feedback/examples.
Frederik
 
F

Frederik

Hi,

I found that I need to overwrite the paint method. This is how I did
this:

class DataGridViewLinkLabelCell : DataGridViewCell
{
private LinkLabel ll = new LinkLabel();

public DataGridViewLinkLabelCell() : base()
{
ll.LinkBehavior = LinkBehavior.NeverUnderline;
ll.Padding = new Padding(2);
ll.AutoSize = true;
}

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)
{
base.Paint(graphics, clipBounds,
cellBounds, rowIndex, cellState,
value, formattedValue, errorText,
cellStyle, advancedBorderStyle,
paintParts);
//ll.Size = cellBounds.Size;
ll.Location = cellBounds.Location;
this.DataGridView.Controls.Add((LinkLabel)this.Value);
}
...
}

But I still don't have exactly what I want (the LinkLabel is added
topleft on the userform). I'll keep looking and when I progress, I'll
update this thread (who knows someone might struggle with the same
issue;-).

Fre
 
F

Frederik

Hi, me again.

The thing I didn't yet figure out is how I can add the controls. I
have an example (code from a user-form with a DataGridView on it):

DataGridViewLinkLabelColumn llc;
llc = new DataGridViewLinkLabelColumn();
llc.HeaderText = "TEST";
dataGridView1.Columns.Add(llc);

LinkLabel ll = new LinkLabel();
ll.Text = "Google versus Microsoft";
ll.Links.Add(0, 6, "www.google.com");
ll.Links.Add(14, 9, "www.microsoft.com");
this.dataGridView1.Rows.Add(ll);

ll = new LinkLabel();
ll.Text = "Poogle versus Nicrosoft";
ll.Links.Add(0, 6, "www.poogle.com");
ll.Links.Add(14, 9, "www.nicrosoft.com");
dataGridView1.Rows.Add(ll);

Problem: the LinkLabel controls don't show up. Do I need to overwrite
something else besides the paint method? I also overwrote "public
override Type ValueType", but that's not doing the trick either. How
do I pass the LinkLabels? It keeps puzzling me.

Fre
 
F

Frederik

After quite some searching, I left the LinkLabel story behind and used
the CellPainting event.
So, in case you want to use substrings with a different color in your
cell, the following works fine:

In the CellPainting event of your DataGridView:

...
Rectangle strRect = e.CellBounds; strRect.Offset(1, 0);
// prevent spacing
TextFormatFlags flags = TextFormatFlags.NoPadding;
Size proposedSize = new Size(int.MaxValue, int.MaxValue);
...
drawSubstring(sub, new SolidBrush(e.CellStyle.ForeColor),
strRect, e);
// update the location where to start writing (drawing)
strRect.X = strRect.X + TextRenderer.MeasureText(e.Graphics,
sub, f, proposedSize, flags).Width;
drawSubstring(sub2, Brushes.Blue, strRect, e);
...
private void drawSubstring(string s, Brush b, Rectangle r,
DataGridViewCellPaintingEventArgs e)
{
StringFormat sf = new StringFormat();
// vertical align in the middle
sf.LineAlignment = StringAlignment.Center;
Font f = dgvCommands.DefaultCellStyle.Font;
e.Graphics.DrawString(s, f, b, r, sf);
}
...

Always feel free to correct me...

Fre
 
R

rehan

I Tried this method to Add the usercontrol to a datagridviewcolumn
and i overrided the paint method of dataGridviewCell to display my control
in design time
my problem is when i have many rows in my datagridview, i found the column
that host my usercontrol continue blinking for awhile until paint all rows
Custom column cells

do you have any solution for this problem ?

Nicholas Paldino said:
Frederik,

What you are doing should be possible with a custom control for a
DataGridView. Here is a link to a sample which hosts a MaskedTextBox:

http://msdn2.microsoft.com/en-us/library/ms180996.aspx

You should be able to adapt this to your needs.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Frederik said:
Hi all,

Can someone tell me how I can host a LinkLabel in a DataGridView (the
existing DataGridViewLinkColumn does not provide what I need). Do I
need to derive from DataGridViewTextBoxCell or from DataGridViewCell?

Many thanks for any feedback/examples.
Frederik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top