I have a datagridview and a bindinglist. They work together pretty ok, but I want to make the properties appear in the rows´ headers, not on the column. Other thing I want to do is to make the headers´ text be different from the properties name. Is there any way to achieve that ? My code for anyone who is interested.

Public Class Form1

Dim listaBindingSource As New BindingList(Of pessoa)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim b1 As New pessoa()
    listaBindingSource.Add(b1)
    dgv.DataSource = listaBindingSource
End Sub

End Class

Public Class pessoa

Dim sells_Month1 As String

Public Sub New() 'ByVal nome_fora As String)

    sells_Month1 = "0"

End Sub

Property vendas1 As String
    Get
        Return sells_Month1
    End Get
    Set(value As String)
        sells_Month1 = value
    End Set
End Property

The other properties are vendas2, vendas3.. and are the same as this one. Thanks, Ricardo S.

Recommended Answers

All 10 Replies

       dgv.DataSource = listaBindingSource
        dgv.Columns(0).HeaderText = "Value"
        dgv.Columns(1).HeaderText = "Date"
        For i = 0 To dgv.Rows.Count - 1
            Dim num As Integer = i + 1
            dgv.Rows.Item(i).HeaderCell.Value = "Vendas" & num
        Next

Hmm... I have a correction to do.

They work together pretty ok, but I want to make the properties appear in the rows´ headers, not on the column.

Should be read as:

They work together pretty ok, but I want to make the properties VALUES APPEAR IN THE ROWS, not on the columns.

Right now I have something like:

Values1 Values2
0 0

And I want,

Values1 0
Values2 0

Any idea ?

But the headers´problem is solved, thanks !

Code :

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Dim b1 As New pessoa()
        listaBindingSource.Add(b1)
        dgv.DataSource = listaBindingSource
        For i = 0 To dgv.Rows.Count - 1
            Dim num As Integer = i + 1
            dgv.Rows.Item(i).HeaderCell.Value = "Values" & num
        Next
    End Sub
End Class

Result :

1cdc994948106ce26d79b9f8ec2a9fa7

I might have given a bad example on my last post. I am not referring to the headers anymore, but for how my objects added are being displayed.

Imagine my class is like this:

    Public Class pessoa
        Dim sells_Month1 As String
        Dim sells_Month2 As String

    Public Sub New()
        sells_Month1 = "0"
        sells_Month2 = "0"
    End Sub

    Property vendas1 As String
        Get
        Return sells_Month1
        End Get
        Set(value As String)
        sells_Month1 = value
        End Set
    End Property

        Property vendas2 As String
        Get
        Return sells_Month2
        End Get
        Set(value As String)
        sells_Month2 = value
        End Set
    End Property

When I apply this to my code I get this:

If I add an object I do have this:

What I want is to have this for one object on my list:

Values1 0                
Values2 0

What I want is to have this for two objects - I add objects with a button.

Values1 0 0            
Values2 0 0

So I add a new list on a new column, not on a new row. I really appreciatte your help, oussama_1, but do you know how to solve this ?

hi, i need to see your code for adding objects (add button)

It´s not anything extraordinary.

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        listaBindingSource.Add(New pessoa)
    End Sub

won't work, use datatable instead of binding
sorry i'm busy right now, if you want the code let me know
good luck

Hmm... But will the bind work with datatables ? I have tried a lot to do that and just couldn´t figured out how. If there is a code, I would be really happy.
Thanks.

Public Class Form1
    Dim dgv As New DataGridView
    Dim txt1 As New TextBox
    Dim txt2 As New TextBox
    Dim label1 As New Label
    Dim label2 As New Label
    Dim add As New Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(563, 224)
        Me.Controls.Add(dgv)
        Me.Controls.Add(txt1)
        Me.Controls.Add(txt2)
        Me.Controls.Add(label1)
        Me.Controls.Add(label2)
        Me.Controls.Add(add)
        label1.Text = "Values 1"
        label2.Text = "Values 2"
        label1.Location = New Point(4, 105)
        label2.Location = New Point(4, 131)
        txt1.Location = New Point(57, 102)
        txt2.Location = New Point(57, 128)
        add.Location = New Point(57, 154)
        add.Text = "Add"
        txt1.Text = "0"
        txt2.Text = "0"
        dgv.Size = New Size(547, 92)
        dgv.Columns.Add("Vendas1", "Vendas1")
        dgv.Rows.Add("0")
        dgv.Rows.Add("0")
        AddHandler add.Click, AddressOf addvalues
    End Sub

    Private Sub addvalues(ByVal sender As System.Object, ByVal e As System.EventArgs)
        dgv.Columns.Add("Vendas" & dgv.Columns.Count.ToString + 1, "Vendas" & dgv.Columns.Count.ToString + 1)
        dgv.Rows(0).Cells("Vendas" & dgv.Columns.Count.ToString).Value = txt1.Text
        dgv.Rows(1).Cells("Vendas" & dgv.Columns.Count.ToString).Value = txt2.Text
    End Sub
End Class

It isn´t close from what a I was asking but was the only one that has given me any attention.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.