none
Add DataGridViewLinkColumn RRS feed

  • Question

  • Hi guys/gals,

    Got an issue with adding a DataGridViewLinkColumn.

    I'm trying to add 4 columns, one of which is a DataGridViewLinkColumn.

    I can add the link column and it works fine using the first code block below. There are two code blocks.

    However, when I save all the column sizes and their locations in the grid when the form closes, the location of the link column is always at the end of the grid.

    Now here's the part I don't understand. I open the form and set the columns where I want them in the grid including their column sizes. Then I click on any of the other headers to stack the data in that column to either ASC or DESC order.

    Now I close the form and reopen the form. All the columns are where they should be and their sizes too and the link colum works like it should.

    Dim links As New DataGridViewLinkColumn()

    With links
    ' Column Header Text
    .HeaderText = mc_sColumnName_Document
    ' Text with the Column Field
    .Text = "Read"
    .UseColumnTextForLinkValue = True
    .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    .ActiveLinkColor = Color.White
    .LinkBehavior = LinkBehavior.SystemDefault
    .LinkColor = Color.Blue
    .TrackVisitedState = True
    .VisitedLinkColor = Color.YellowGreen
    .Width = mc_iColumnWidth_Document
    .Resizable = DataGridViewTriState.False
    End With

    DataGridView_Main.Columns.Add(links)

    Then I run this code for the other columns.

                With DataGridView_Main

                    .SuspendLayout()

                    iColumn = 0

                    m_sFieldName = CStr("AutoID")

                    sColumnName_Label = mc_sColumnName_AutoID

                    .Columns(iColumn).Name = sColumnName_Label 'm_sFieldName.ToString
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.False

                    iColumn = CByte(iColumn + 1)

                    m_sFieldName = CStr("Unique_Name")

                    sColumnName_Label = "UniqueName"

                    .Columns(iColumn).Name = sColumnName_Label
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.True

                    iColumn = CByte(iColumn + 1)

                    m_sFieldName = CStr("Color_Style")

                    sColumnName_Label = "Color"

                    .Columns(iColumn).Name = sColumnName_Label
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.True

                    .Columns(iColumn).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

                    .ResumeLayout()

                End With

    Second code block:

    If I run this code block it works as expected (poistion and size of columns are saved and open properly), however, when I click in the link column to open a document it says column name doesn't exist.

                With DataGridView_Main

                    .SuspendLayout()

                    iColumn = 0

                    m_sFieldName = CStr("AutoID")

                    sColumnName_Label = mc_sColumnName_AutoID
                    .Columns(iColumn).Name = sColumnName_Label 'm_sFieldName.ToString
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.False

                    iColumn = CByte(iColumn + 1)

                    m_sFieldName = CStr("Unique_Name")
                    sColumnName_Label = "UniqueName"
                    .Columns(iColumn).Name = sColumnName_Label
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.True

                    iColumn = CByte(iColumn + 1)

                    m_sFieldName = CStr("Color_Style")
                    sColumnName_Label = "Color"
                    .Columns(iColumn).Name = sColumnName_Label
                    .Columns(iColumn).DataPropertyName = m_sFieldName
                    .Columns(iColumn).Resizable = DataGridViewTriState.True

                    .Columns(iColumn).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

    Dim links As New DataGridViewLinkColumn()

    With links
    ' Column Header Text
    .HeaderText = mc_sColumnName_Document
    ' Text with the Column Field
    .Text = "Read"
    .UseColumnTextForLinkValue = True
    .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    .ActiveLinkColor = Color.White
    .LinkBehavior = LinkBehavior.SystemDefault
    .LinkColor = Color.Blue
    .TrackVisitedState = True
    .VisitedLinkColor = Color.YellowGreen
    .Width = mc_iColumnWidth_Document
    .Resizable = DataGridViewTriState.False
    End With

    DataGridView_Main.Columns.Add(links)

                    .ResumeLayout()

                End With

    HELP!

    Thanks!


    Sunday, September 10, 2017 2:28 PM

All replies

  • Hi

    Is there a question in there somewhere?

    BTW: use the code block tool (from the toolbar) to post code.


    Regards Les, Livingston, Scotland

    Sunday, September 10, 2017 2:41 PM
  • Sorry,

    Not sure that a question was needed... I explain what I needed and what it was and wasn't doing.

    But here-goes...

    How to add link column where when I save the grid attributes on form close (column index (its location in the grid), column size) it saves it properly. Also, how to get the link column to open a document when I select the link column. It says the column name doesn't exist but it does exist.

    Thanks.


    ADawn


    Sunday, September 10, 2017 3:01 PM
  •  I did not look too hard or test the code but,  knowing that the Name property of a control is usually considered invalid if it contains any spaces,  then the two lines like below could show a possible problem.  Notice you have a space in the "Unique Name" string which you are using for the Column's Name property.

                    sColumnName_Label = "Unique Name"
                    .Columns(iColumn).Name = sColumnName_Label


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, September 10, 2017 3:20 PM
    Sunday, September 10, 2017 3:18 PM
  • IronRazerz

    Somehow I got a space between [Unique Name] when I posted here. That's not the problem. I updated the original code here above.

    Thanks.


    ADawn

    Sunday, September 10, 2017 4:00 PM
  • Hi,

    My guess is without someone taking your code and test it on their computer it's hard to say what the failure point is but one of the current replies may be the solution.

    Personally I would so less code to setup the DataGridView and do as much as possible in the designer.

    If open to this, check out the following code sample which uses a custom DataGridView done in C# to remember column widths and column positions.

    https://1drv.ms/u/s!AtGAgKKpqdWjiGf0ZfZo0_BgD7lt

    Main project is VB.NET while the customer DataGridView is C# used in the VB.NET project.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, September 10, 2017 7:41 PM
    Moderator
  • Hello Kareninstructor,

    I need to create this grid programmically.

    When I close the form the grid information (locations in the grid and their sizes) are saved as expected using the second code example above.

    However, when I search for the (linked column name) during CellFormatting, it tells me no such name exists. It's so strange.

    ADawn


    ADawn

    Monday, September 11, 2017 5:48 PM
  • Yes I understand you are doing this in code. What I did writes code for you and was suggesting you try it out this way. The only time one should be writing code as you did is if you are configuring the DataGridView in a code module or class. No matter, I could write what I did inside the form, class or code module with the same exact results but no going too as it would be a waste of time when the method I used does it for me.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 11, 2017 10:28 PM
    Moderator