Link to home
Start Free TrialLog in
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) ToutountzoglouFlag for Greece

asked on

ListBox Items As Header Text in a Datagridview Columns

I have a databound ListBox .I need to pass the items as header text in my datagridView Columns.
i use the follwing code

Dim drv as datarowview =Ctype(me.Listbox1.SelectedItem,Datarowview)
dim SelTex as String=drv.Item("ColumnItems")
Dim TNumber as integer =Me.Listbox.Items.Count
   'For the Datagridview i set the visibility of the Desired Columns to true'
    
     For i as integer =3 to (3+TNumber)-1 
         Me.Datagridview1.Columns.Item(i).Visible=true
         Me.Datagridview1.Columns.item(i).HeaderText=SelText     'And Here Is My Problem I got Only the First item .I know that i have to loop through the lisbox items but......'
      next

Open in new window

Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

See this one:

        Dim i As Integer = 0
        For i = 0 To DataGridView1.Columns.Count - 1
            'use one of these lines
            DataGridView1.Columns(i).HeaderText = ListBox1.Items(i).ToString()        ' if items non-selected
            DataGridView1.Columns(i).HeaderText = ListBox1.SelectedItem.ToString()    ' if one item selected  
        Next



Avatar of John (Yiannis) Toutountzoglou

ASKER

Shahan_Dev…
that is why i 'am using drv as datarowview because with your code the header text goes to system.data.datarowview
Hi!
your this line was giving me the casting error:
Dim drv as datarowview =Ctype(me.Listbox1.SelectedItem,Datarowview)

So I used my methodology to do the same but said it is not what you want.
 change your line 8 with this and see what happened:
Me.Datagridview1.Columns(i).HeaderText=SelText
 
If you can post the datagridview's image before and after this code affected then it can help to trace the problem.
I've done the way you told and i got the same results...  Me.Datagridview1.Columns(i).HeaderText=Me.ListBox1.Items(i).ToString()
the header text of datagridviewColumns renamed and shows System.Data.DatarowView
Hi!

I am unable to understand your problem. If you will show me your ListView values then I may be could do something, because I have a string in listview which is not convertable in DataRow view so it is giving error.

Also, what do you mean about :

>>> with your code the header text goes to system.data.datarowview
I think that i have to rephrase the Question ...
Let's say that you have a datagridview.
the Columns after Column(0) displays numbers that are Primary Keys or indexes of a table..(For example Table with 2 Columns Key and descrisption)
how can i replace these numbers with the description and put it as datagridview column header text..
Why am i doing this ...
Because using Greek Language when i am trying to set the  column headers with the greek letters directly from datasource (Sql Database) the column headers shows "??????????"
So i am trying to find another way to solve this problem ....
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok that really helps....i got it and thank you very much...