joynerCN

Pardon me for posting twice in quick succession, but I've always found that if I post two questions in a thread, only one gets any attention.

I've got a DataGridView where each line displays information from a class called 'Flag', which has a number of variables. One of these variables is a URL for a page that the particular 'Flag' pertains to. But rather than displaying the URL in a column, I'd like to display a clickable link that just says "Video" and, when clicked, opens a new window with the URL.

How can this be done The only things I'm familiar with on DataGridViews are .DataPropertyName and .Name, so I don't have any idea how to set all the text for a column to one text value, or how to handle the click event of each individual link.



Re: Visual Basic Language Question on DataGridView Data

ShyamN

Maybe the following link would help

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

I think 'DataGridViewLinkColumn' should do the trick.

Shyam






Re: Visual Basic Language Question on DataGridView Data

joynerCN

Well, my problem there is that there's still a few things I don't know:

I don't know how to make every cell in the column have the same visible text.
I don't know how to make the cells exhibit the behavior I want (to show a Video form I've designed after giving it a specific URL).





Re: Visual Basic Language Question on DataGridView Data

ShyamN

The below code should hopefully answer both your questions.

Also - another useful article I came across - http://www.awprofessional.com/articles/article.asp p=446453&seqNum=7&rl=1

Hope this helps.

Shyam

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim flags As Flag() = New Flag() {New Flag, New Flag} 'Flag objects

'Set up your datagrid with the right columns (you could also do this using the designer)

Dim col1 As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()

Dim col2 As DataGridViewLinkColumn = New DataGridViewLinkColumn

DataGridView1.Columns.Add(col1)

DataGridView1.Columns.Add(col2)

'Hookup callback - Make cells exhibit the behavior you want (e.g. show a Video)

AddHandler DataGridView1.CellContentClick, AddressOf ShowVideo

'The below loop adds rows to the DataGridView manually

For Each f As Flag In flags

Dim cell1 As New DataGridViewTextBoxCell

cell1.Value = f.someField

Dim cell2 As New DataGridViewLinkCell

cell2.Value = "Video" 'To make all columns have same visible text

cell2.Tag = f.url 'Store the url in the Tag field

Dim row As New DataGridViewRow

row.Cells.Add(cell1)

row.Cells.Add(cell2)

DataGridView1.Rows.Add(row)

Next

'DataBinding could have been used making things really simple

'i.e. there would have been no need to create rows and and cells manually

'But I could not figure out how to make DataBinding work so that the link text and url are different

'To see how DataBinding would work

' - Comment out the loop code above

' - Uncomment the 3 lines of code below

' - Make the change noted in the 'OpenVideo' method below

'col1.DataPropertyName = "someField"

'col2.DataPropertyName = "url"

'DataGridView1.DataSource = flags

End Sub

Sub ShowVideo(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

If TypeOf DataGridView1.Item(e.ColumnIndex, e.RowIndex) Is DataGridViewLinkCell Then

Process.Start("C:\Program Files\Internet Explorer\iexplore.exe", DataGridView1.Item(e.ColumnIndex, e.RowIndex).Tag)

'To see how DataBinding would work

' - Comment out the above line

' - Uncomment the below line

'Process.Start("C:\Program Files\Internet Explorer\iexplore.exe", DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)

End If

End Sub

End Class

Class Flag

Private _someField As Integer = 1

Private _url As String = "www.live.com"

Public Property someField() As Integer

Get

Return _someField

End Get

Set(ByVal value As Integer)

_someField = value

End Set

End Property

Public Property url() As String

Get

Return _url

End Get

Set(ByVal value As String)

_url = value

End Set

End Property

End Class






Re: Visual Basic Language Question on DataGridView Data

joynerCN

Getting close - most of that works, but the last thing I'm having trouble with is having every cell in that column have the same text, a label that says 'Video'.

Thing is, Rows are being added manually to this DataGrid fairly frequently, so just manually going through and setting every current cell in the column to 'Video' won't work given that it'd need to be redone everytime a row was added.

The extremely unorthodox way of doing it that I can think of is giving every flag a member variable of type String with the text 'Video'. That way I could use the DataPropertyName for the text; but that's kinda a ridiculous way to do this.

I'm not using Tags, instead I'm detecting what row the clicked cell was in and calling that indexed variable in the list of Flags.

There's gotta be a fairly normal way to set all text, current and future, in a column equal to the same thing, doesn't there





Re: Visual Basic Language Question on DataGridView Data

ShyamN


Sorry for the delayed reply. The properties UseColumnTextForLinkValue and Text on the DataGridViewLinkColumn class should be able to help you do this. I had tried this - but I could not figure out a way to update both the tag and the link text i.e. while the text would be correctly set, the tag would'nt. Maybe I didn't try hard enough so you could try giving this a shot.

Another suggestion is that you could probably try just adding the new rows each time you need to add rows - rather than iterating over all rows - (if this is possible in your design). Calling DataGridView.Add would append the supplied new row and would keep existing rows intact. This would mean that you don't have to update all cells with text 'Video'.

Hope this helps,
Shyam






Re: Visual Basic Language Question on DataGridView Data

joynerCN

Woo! That worked, thanks!