Url in datagridview

In VS2005 WINFORMS C# datagridview:
I have a cell in the datagridview that is a URL address and I want that when oressinf this cell It will
Link to the Address in it.
How do I accomplish this?
Thankyou
Anat
ANAT2403Asked:
Who is Participating?
 
ToFroConnect With a Mentor Commented:
I renamed cellContent to url, since that's what it is.

Calling System.Diagnostics.Process.Start(url); is the equivalent of clicking on start, run, typing in the url and hitting enter. In other words, it starts the default web browser and opens the url.

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn && e.RowIndex != -1)
            {
                string url = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                System.Diagnostics.Process.Start(url);
            }
        }
0
 
ToFroCommented:
The DataGridView has a ColumnType: DataGridViewLinkColumn, have you tried that?
0
 
ToFroCommented:
Here's a link to the MSDN entry for DataGridViewLinkColumn:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewlinkcolumn.aspx
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ANAT2403Author Commented:
Hi,
I have a datatable dt and I linked the gridview to this dt.
Now How do I change the columntype of a certain column to be a DataGridViewLinkColumn in the code?
Thankyou
Anat
0
 
ToFroCommented:
In Visual Studio, when the DataGridView is selected, there is a small arrow in the top right corner of the control. Click it and select "Edit Columns...". If you haven't added any columns to the DataGridView(displays all columns by default), add one for every column in the DataTable. In the "Add Column" dialog there is a Type option. Set this option to DataGridViewLinkColumn for the link column.

After you have added all the columns, go through them one by one and set their DataPropertyName to the name of the corresponding column in the DataTable.
0
 
ANAT2403Author Commented:
Hi,
You don't understand what I am asking!
I set the data of the gridview during running not in advance so I want to set it in code not with the control. I am familiar to what you wrote.
0
 
ToFroCommented:
Let's say you have two columns in the DataTable, id and link. In that case you could do this while setting the DataSource for the DataGridView Control:

            dataGridView1.Columns.Clear();        // In case you switch between several data sources
            DataGridViewTextBoxColumn dgvColId = new DataGridViewTextBoxColumn();
            DataGridViewLinkColumn dgvColLink = new DataGridViewLinkColumn();
            dgvColId.DataPropertyName = "id";     // The name of the id column in the data source
            dgvColLink.DataPropertyName = "link";   // The name of the link column in the data source
            dataGridView1.Columns.Add(dgvColId);
            dataGridView1.Columns.Add(dgvColLink);
            dataGridView1.DataSource = table;
0
 
ANAT2403Author Commented:
Hi,
This is very good  and close to what I need but...
My situation is that there is a datatable that include for example 3 fields:
id        int
name  string
url      string
The gridview automaticaly read the type of the fields from the datatable and set them as DataGridViewTextBoxColumn. After this process is finished I want to change the url column to be a
DataGridViewLinkColumn  instead of DataGridViewTextBoxColumn .
Is that possible?
Thankyou
Anat
0
 
ToFroCommented:
You can let the DataGridView automatically create the columns by just setting the DataSource,
and then remove the link column by index and create a new one line this:

            dataGridView1.DataSource = table;
            dataGridView1.Columns.Remove(dataGridView1.Columns[2]);  //  2 is the index of the col
            DataGridViewLinkColumn dgvLinkCol = new DataGridViewLinkColumn();
            dgvLinkCol.Name = "linkCol";
            dgvLinkCol.DataPropertyName = "link";
            dataGridView1.Columns.Add(dgvLinkCol);

Personally, I would create all the columns for a more predictable result, and more obvious code. But you can do it however you like. Isn't that great.
0
 
ANAT2403Author Commented:
Hi,
I have a datatable dt and I write :
datagridview1.datasource = dt;
I I do what you write I will loose my data.
Anat
0
 
ToFroCommented:
> I I do what you write I will loose my data.

What do you mean by loose your data? Are you afraid to try the code? It just removes the column from the DataGridView, not the data from the DataTable, and certainly not the data from the source.

When you add the new column and assign the DataPropertyName to the name of the column in the source, the data will reappear.
0
 
ANAT2403Author Commented:
Hi,
I apologize. I did try what you wrote before and it didn't show the data but now I tried it again and it worked O.K.
2 small problems left:
1. after I removed the column and add it again it appear now as the last field. How do I show the previous order of the fields.
2. If I want the link to work do I have to set any property? because now it show the hand but don't link to the address.
Thankyou
Anat
0
 
ToFroCommented:
1. Assuming the LinkColumn is the third column:
    dataGridView1.Columns[2].DisplayIndex = 0;

    You could also specify the .Name property of the columns, then you wouldn't have to use the index
    value. You might want to set the HeaderText properties while you are at it.

2. You would have to handle the CellContentClick Event of the DataGridView and do something like this:

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn && e.RowIndex != -1)
            {
                string cellContent = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                MessageBox.Show(cellContent);
            }
        }
0
 
ANAT2403Author Commented:
Hi,
In the second part how do I link to the address?
thankyou
Anat
0
 
ANAT2403Author Commented:
Hi ToFro,
I want to thank you a lot for helping me in all this subject.
Your answers finally where perfect.
Thankyou
Anat
0
 
ToFroCommented:
You're welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.