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 General / February 2005

Tip: Looking for answers? Try searching our database.

how to put LinkLabel in DataGrid's column

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maulin Vasavada - 09 Feb 2005 05:15 GMT
Hi,

I am trying to develop Windows Forms application (not Web.UI stuff),
and
I wanted to put LinkLabel in the DataColumn of DataGrid but I am not
able to do so. I got this below link where the guy extended
DataGridColumnStyle to have combobox but I can't figure out how to put
LinkLabel instead of combobox used there.
http://www.c-sharpcorner.com/winforms/ComboBoxInDataGridSKJ.asp

The issue with that example code is I have to setup too many things to
run it as its using Sql. I appreciate his sharing but I wish he would
have done it more simplistically to show an example rather than having
those sql things in there.

I copied his code and all and tried to understand by putting Enum
object in combobox but all in vein...though I now understand those
DataSet and all upto some extent. So at the end I decided to extend the

DataGridColumnStyle to put LinkLabel and copied Paint method from his
code and have mixed things right now. Here is the code which is not
working, :)

namespace TestUIApplication
{
       public class LinkColumn:DataGridColumnStyle
       {
               int xMargin = 5;
               int yMargin = 5;

               private LinkLabel linkLabel;

               //
               // Create a new column - DisplayMember,
ValueMember passed by string
               //
               public LinkColumn(LinkLabel linkLabel)
               {
                       this.linkLabel = linkLabel;
               }

               protected override void Abort(int RowNum)
               {
                       MessageBox.Show(new Form(),"in
Abort()");
               }
               protected override bool Commit(CurrencyManager
DataSource,int RowNum)
               {
                       MessageBox.Show(new Form(),"in
Commit()");
                       return true;
               }
               protected override void ConcedeFocus()
               {
                       MessageBox.Show(new Form(),"in
ConcedeFocus()");
               }
               protected override void Edit(CurrencyManager
Source ,int
Rownum,Rectangle Bounds, bool ReadOnly,string InstantText, bool
CellIsVisible)
               {
                       MessageBox.Show(new Form(),"in
Edit()");
               }
               protected override int GetMinimumHeight()
               {
                       return linkLabel.PreferredHeight +
yMargin;
               }
               protected override int
GetPreferredHeight(Graphics g ,object Value)
               {
                     
System.Diagnostics.Debug.WriteLine("GetPreferredHeight()");
                       return FontHeight+yMargin;
               }
               protected override Size
GetPreferredSize(Graphics g, object Value)
               {
                     
MessageBox.Show("GetPreferredSize():"+linkLabel.Text);
                       Size Extents =
Size.Ceiling(g.MeasureString(linkLabel.Text,
this.DataGridTableStyle.DataGrid.Font));
                       Extents.Width += xMargin * 2 +
DataGridTableGridLineWidth ;
                       Extents.Height += yMargin;
                       return Extents;
               }
               protected override void Paint(Graphics
g,Rectangle
Bounds,CurrencyManager Source,int RowNum)
               {
                       Paint(g, Bounds, Source, RowNum,
false);
               }
               protected override void Paint(Graphics
g,Rectangle
Bounds,CurrencyManager Source,int RowNum,bool AlignToRight)
               {
                       string Text = linkLabel.Text;
                       PaintText(g, Bounds, Text,
AlignToRight);
               }

               private void PaintText(Graphics g ,Rectangle
Bounds,string Text,bool
AlignToRight)
               {
                       Brush BackBrush = new
SolidBrush(this.DataGridTableStyle.BackColor);
                       Brush ForeBrush= new
SolidBrush(this.DataGridTableStyle.ForeColor);
                       PaintText(g, Bounds, Text,
BackBrush, ForeBrush, AlignToRight);
               }
               private void PaintText(Graphics g , Rectangle
TextBounds, string
Text, Brush BackBrush,Brush ForeBrush,bool AlignToRight)
               {
                       System.Console.WriteLine("in
paint");
                       Rectangle Rect = TextBounds;
                       RectangleF RectF  = Rect;
                       StringFormat Format = new
StringFormat();
                       if(AlignToRight)
                       {
                               Format.FormatFlags =
StringFormatFlags.DirectionRightToLeft;
                       }
                       switch(this.Alignment)
                       {
                               case
HorizontalAlignment.Left:
                                     
Format.Alignment = StringAlignment.Near;
                                       break;
                               case
HorizontalAlignment.Right:
                                     
Format.Alignment = StringAlignment.Far;
                                       break;
                               case
HorizontalAlignment.Center:
                                     
Format.Alignment = StringAlignment.Center;
                                       break;
                       }
                       Format.FormatFlags
=Format.FormatFlags;
                       Format.FormatFlags
=StringFormatFlags.NoWrap;
                       g.FillRectangle(BackBrush, Rect);
                       Rect.Offset(0, yMargin);
                       Rect.Height -= yMargin;
                       g.DrawString("maulin",
this.DataGridTableStyle.DataGrid.Font,
ForeBrush, RectF, Format);
                       Format.Dispose();
               }

               private int DataGridTableGridLineWidth
               {
                       get
                       {
                             
if(this.DataGridTableStyle.GridLineStyle ==
DataGridLineStyle.Solid)
                               {
                                       return 1;
                               }
                               else
                               {
                                       return 0;
                               }
                       }
               }
       }

}

And here is the method that loads things,

private void Form1_Load(object sender, System.EventArgs e)
               {
                       listBox1.DataSource =
this.coloursCollection;

                       DataSet ds = new
DataSet("ColoursSet");
                       DataTable dt =
ds.Tables.Add("ColoursTable");
                     
dt.Columns.Add("ColoursColumn",typeof(LinkLabel));

                       LinkLabel ll = new LinkLabel();
                       ll.Name = "maulin";
                       ll.Text ="Maulin";

                       DataRow dr = dt.NewRow();
                       dr["ColoursColumn"] = ll;
                       dt.Rows.Add(dr);

                       GridTableStyle = new
DataGridTableStyle();
                       GridTableStyle.MappingName =
"ColoursSet";

                     
GridTableStyle.GridColumnStyles.Add(new LinkColumn(ll));
                     
GridTableStyle.GridColumnStyles[0].MappingName =
"ColoursTable";//EDIT ME
                     
GridTableStyle.GridColumnStyles[0].HeaderText = "test";
                     
GridTableStyle.GridColumnStyles[0].Alignment =
HorizontalAlignment.Left;
                     
GridTableStyle.GridColumnStyles[0].Width = 700;
                     
GridTableStyle.GridColumnStyles[0].NullText = string.Empty;

                       dataGrid1.DataSource = ds;
                       dataGrid1.DataMember =
"ColoursTable";
                     
dataGrid1.TableStyles.Add(GridTableStyle);
               }

Please ignore my table,column names and all , its all the code after
much playing around so far and names might not make sense...

Do you have any idea how to make this work? Any help would be of great
help.

Please forive my obvious mistakes as I am new to C#.

Regards,
Maulin
Maulin Vasavada - 09 Feb 2005 22:07 GMT
Nobody has any input? Please try to see if you can help. I am stuck on
that for a week.
Maulin Vasavada - 12 Feb 2005 10:49 GMT
This is unbelievable. I don't think its a very difficult problem. I am
going to try my best to solve it and let ppl here know if I find a
solution or even if I don't.

Didn't anybody need this ever?

Regards,
Maulin

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.