Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I use these codes at OnCreateControl to add DataGridViewLinkColumn.
But I can't set text for column(not headerText, just text).

Note: When I add column by designer there is property named Text but inside below code it is not.

C#
DataGridViewLinkCell cell = new DataGridViewLinkCell();
cell.UseColumnTextForLinkValue = true;

DataGridViewColumn column = new DataGridViewColumn();
column.CellTemplate = cell;
// column.Text dose not exists
this.Columns.Add(column);
Posted
Comments
Kenneth Haugland 29-Jul-12 14:13pm    
column.Text wont exist but Column.Header might... The only field that would have a possible tetblock in it would be the DataGridViewLinkCell, so Ill bet you can find cell.text or?
[no name] 29-Jul-12 14:45pm    
I had tried cell.Text too, but I didn't find cell.Text either.
Kenneth Haugland 29-Jul-12 15:08pm    
IT this WinForms or WPF?
[no name] 29-Jul-12 16:05pm    
It's WinForms.

1 solution

Please revise if you see in designer the columns or rows. Columns haven't text, because they have differents data in each cell (normally). I can not found "text" in the designer, you can also only give the tooltip and header text.

If you want to have a default value in the column, you must create a derivated class from DataGridViewLinkCell and override the DefaultNewRowValue

see the following example (dirty example, you should create a new file for the derived class):

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewLinkCell cell = new NewDataGridViewLinkCell();
            cell.UseColumnTextForLinkValue = true;
            
            DataGridViewColumn column = new DataGridViewColumn();
           column.CellTemplate = cell;

            // column.Text dose not exists
            dataGridView1.Columns.Add(column);
            dataGridView1.Rows.Add(new DataGridViewRow());
            
        }
    }

    public class NewDataGridViewLinkCell : DataGridViewLinkCell
    {
        public override object DefaultNewRowValue
        {
            get
            {
                // Enter here your text.
                return "My Text";
            }
        }

    }


Observe that now, you instance NewDataGridViewLinkCell and you can enter a predefined test each time that a new row is created.
 
Share this answer
 
Comments
[no name] 1-Aug-12 20:33pm    
Not designer.cs, I meant Form.cs[Design]. In the edit Columns window when you add a DataGridViewLinkCell type column there is a property named Text.
My purpose is to set text for link not cell.
Sorry it was my fault. Bad explaining.
I tried your code, it didn't work.
freedeveloper 1-Aug-12 23:54pm    
When you run this code you dont see the "My Text" in column? To enter a URL link, you must enter a URL link, and override the DefaultNewRowValue as a url.
You can see more information at: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.defaultnewrowvalue.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900