ListBox Items As Header Text in a Datagridview Columns

John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shahan AyyubSenior Software Engineer

Commented:
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



John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Author

Commented:
Shahan_Dev…
that is why i 'am using drv as datarowview because with your code the header text goes to system.data.datarowview
Shahan AyyubSenior Software Engineer

Commented:
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
 
Shahan AyyubSenior Software Engineer

Commented:
If you can post the datagridview's image before and after this code affected then it can help to trace the problem.
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Author

Commented:
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
Shahan AyyubSenior Software Engineer

Commented:
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
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Author

Commented:
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 ....
Senior Software Engineer
Commented:
If I understood correctly, you want to Rename Columns Herader of DataGridView.Column HearerText with the item in the ListView. So, If you have 8 items in list you have 8 columns Correct ?? you are having difficulty in retrieving them ??

See this code:

This code retrieves the selected and onwords items and rename the HeaderText Accordingly.


If this does not help you kindly let me know which correction is needed.

Dim drv As DataRowView = CType(Me.ListBox1.SelectedItem, DataRowView)
Dim SelTex As String = drv.Item("Description")
Dim TNumber As Integer = Me.ListBox1.Items.Count
'For the Datagridview i set the visibility of the Desired Columns to true'

For i As Integer = 0 To 1
   Me.DataGridView1.Columns.Item(i).Visible = True
   Me.DataGridView1.Columns.Item(i).HeaderText = SelTex     'And Here Is My Problem I got Only the First item .I know that i have to loop through the lisbox items but......'
   drv = CType(Me.ListBox1.Items.Item(ListBox1.SelectedIndex + 1), DataRowView) 'To select the item next to selecteditem
   SelTex = drv.Item("Description") 'retrieve element from index
Next

Open in new window

before.PNG
after.PNG
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Author

Commented:
Ok that really helps....i got it and thank you very much...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial