none
Use DGV column Name in a Class RRS feed

  • Question

  • HI all

    in a WInForm project I have a DataGridView (DGV) with several columns.

    Each Column has a Name given at Design Time

    Then I create an istance of a Class passing the DGV to it in the New constructor

    In the class I would like to use the DGV column Name to change some column Property,but in the class if I try to use the column name I get error since the Column Name is not declared.

    So, how can I use Column Name in the class 

    Friday, November 9, 2018 4:36 PM

Answers

  • Sorry for the late replay.

    I prefer to avoid to write DGV column Name in a string like 

    d.ChangeSomething("DataGridViewTextBoxColumn2", "Hello")
    I do not understand why passing a DGV to a Class I cannot use the DGV Design name of every Column  as I can do in the Main Form 

    That was a literal example, in the wild it would be get the names and work with them.

    Dim columnNames = DataGridView1.Columns.
        Cast(Of DataGridViewColumn).
        Select(Function(col) col.Name).
        ToList


    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

    • Marked as answer by Claudio111 Thursday, November 22, 2018 7:54 PM
    Monday, November 19, 2018 7:40 PM
    Moderator

All replies

  • Here is a down and dirty proof of concept

    Public Class Form3
        Private Sub CategoriesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CategoriesBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.CategoriesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CategoriesTableAdapter.Fill(Me.NorthWindAzureDataSet.Categories)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim d As New Demo(CategoriesDataGridView)
            d.ChangeSomething("DataGridViewTextBoxColumn2", "Hello")
        End Sub
    End Class
    Public Class Demo
        Private _dgv As DataGridView
        Public Sub New(ByVal dgv As DataGridView)
            _dgv = dgv
        End Sub
        Public Sub ChangeSomething(ByVal columnName As String, ByVal headerText As String)
            If _dgv.Columns.Contains(columnName) Then
                _dgv.Columns(columnName).HeaderText = headerText
            End If
        End Sub
    End Class


    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

    • Marked as answer by Claudio111 Sunday, November 11, 2018 12:52 PM
    • Unmarked as answer by Claudio111 Monday, November 19, 2018 5:43 PM
    Friday, November 9, 2018 4:50 PM
    Moderator
  • Hello,

    What I have done is I have created one class through which I am changing my datagridview column header text <Can access any column property in class> present on my form.

    Is this what you require ? If so then below code may help you

    Public Class TestClass
        Public Sub ChangeGridColumnName(ByRef dg As DataGridView)
            Try
                dg.Columns(0).HeaderText = "New Name"
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Try
                Dim obj As New TestClass
                obj.ChangeGridColumnName(DataGridView1)
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub

    Friday, November 9, 2018 4:53 PM
  • Sorry for the late replay.

    I prefer to avoid to write DGV column Name in a string like 

    d.ChangeSomething("DataGridViewTextBoxColumn2", "Hello")
    I do not understand why passing a DGV to a Class I cannot use the DGV Design name of every Column  as I can do in the Main Form 

    Monday, November 19, 2018 5:47 PM
  • Instead of passing the grid view, you can pass the required columns. (The parent grid view can be obtained from DataGridView property of any column).



    • Edited by Viorel_MVP Monday, November 19, 2018 6:51 PM
    Monday, November 19, 2018 6:50 PM
  • Hi Viorel

    The Main Form has the DGV Boun to DataTable then by a Button I have to modify a DGV row.

    So  I open a different Form with several textBoxs that get text value form DGV.

    In order to avoid to make mistake with string I wish to set textBox.text in this way

    TextBox1.text = DGV.Row(currentrow).columns(ColumnName).value

    where ColumnName is the Name given to each DGV Column At Design Time

    It seems no possible :-((((

    Monday, November 19, 2018 7:02 PM
  • Sorry for the late replay.

    I prefer to avoid to write DGV column Name in a string like 

    d.ChangeSomething("DataGridViewTextBoxColumn2", "Hello")
    I do not understand why passing a DGV to a Class I cannot use the DGV Design name of every Column  as I can do in the Main Form 

    That was a literal example, in the wild it would be get the names and work with them.

    Dim columnNames = DataGridView1.Columns.
        Cast(Of DataGridViewColumn).
        Select(Function(col) col.Name).
        ToList


    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

    • Marked as answer by Claudio111 Thursday, November 22, 2018 7:54 PM
    Monday, November 19, 2018 7:40 PM
    Moderator
  • Hi Karen

    Sorry but I do not understand what to do with columnNames List

    How can I use in this statment ?

    TextBox1.text = DGV.Row(currentrow).columns(ColumnName).value

    Tuesday, November 20, 2018 9:47 AM
  • Hi Karen

    Sorry but I do not understand what to do with columnNames List

    How can I use in this statment ?

    TextBox1.text = DGV.Row(currentrow).columns(ColumnName).value

    You would use one of them as ColumnName.

    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

    Tuesday, November 20, 2018 2:10 PM
    Moderator