Share via

Databind Datagridview Headertext ?

Hobbyist_programmer 621 Reputation points
2021-07-31T08:46:14.487+00:00

Hi,

I am looking for a method to bind Headertext property of the datagirdview column to another property. for example , i have currency list (EUR, USD, etc) if currency is changed i want to display in datagridview header Total (EUR) or Total (USD).

Thanks

Developer technologies | VB

1 answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-08-02T03:25:22.143+00:00

    Hi @Hobbyist_programmer ,
    Here is a simple example you can refer to.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim dataTable As DataTable = New DataTable  
            dataTable.Columns.Add("ID")  
            dataTable.Columns.Add("Name")  
            dataTable.Columns.Add("Total")  
            dataTable.Rows.Add(1, "A", 10)  
            dataTable.Rows.Add(2, "B", 11)  
            dataTable.Rows.Add(3, "C", 12)  
            DataGridView1.DataSource = dataTable  
        End Sub  
      
        Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged  
            Dim count As Integer = 0  
            If e.ColumnIndex = 2 AndAlso e.RowIndex >= 0 Then  
                Dim lst = Me.DataGridView1.Rows.Cast(Of DataGridViewRow)().Select(Function(row) row.Cells.Item(2).Value)  
                For Each value In lst  
                    If value IsNot Nothing Then  
                        count += Convert.ToInt32(value)  
                    End If  
                Next  
                DataGridView1.Columns.Item(2).HeaderText = count  
            End If  
        End Sub  
    

    Besides, if I have any misunderstanding, please provide more details here.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.