[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Url in datagridview

Posted on 2007-07-29
16
Medium Priority
?
1,663 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:ANAT2403
  • 9
  • 7
16 Comments
 
LVL 6

Expert Comment

by:ToFro
ID: 19587633
The DataGridView has a ColumnType: DataGridViewLinkColumn, have you tried that?
0
 
LVL 6

Expert Comment

by:ToFro
ID: 19587638
Here's a link to the MSDN entry for DataGridViewLinkColumn:

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

Author Comment

by:ANAT2403
ID: 19590892
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 6

Expert Comment

by:ToFro
ID: 19591018
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
 

Author Comment

by:ANAT2403
ID: 19591208
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
 
LVL 6

Expert Comment

by:ToFro
ID: 19591416
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
 

Author Comment

by:ANAT2403
ID: 19591533
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
 
LVL 6

Expert Comment

by:ToFro
ID: 19591664
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
 

Author Comment

by:ANAT2403
ID: 19591950
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
 
LVL 6

Expert Comment

by:ToFro
ID: 19592043
> 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
 

Author Comment

by:ANAT2403
ID: 19592323
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
 
LVL 6

Expert Comment

by:ToFro
ID: 19592781
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
 

Author Comment

by:ANAT2403
ID: 19593038
Hi,
In the second part how do I link to the address?
thankyou
Anat
0
 
LVL 6

Accepted Solution

by:
ToFro earned 2000 total points
ID: 19594110
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
 

Author Comment

by:ANAT2403
ID: 19595485
Hi ToFro,
I want to thank you a lot for helping me in all this subject.
Your answers finally where perfect.
Thankyou
Anat
0
 
LVL 6

Expert Comment

by:ToFro
ID: 19599068
You're welcome
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question