can't get data to show with bound columns

H

headware

I'm trying to create a DataGrid filled with values from a DataSet and
I want the user to be able to show and hide whichever columns they
choose. I have a dropdown list box filled with the column names so the
user can select a column name and hit a button to hide or show it.
I originally tried to use an unbound DataGrid with autogenerated
columns, however the DataGrid.Columns collection is empty when all the
columns are autogenerated (which seems unnecessarily unfriendly to me,
but that's beside the point). As a result the table displayed the
correct data but I had no way to access the columns in order to hide
them.
So I tried to set up explicitly bound columns. I changed the
"autogenerate columns" property to false and specified the same amount
of columns in the DataGrid as the DataSet was returning. Now, I have
the opposite functionality. I can get the columns to show and hide but
the data isn't showing up in the grid. It has no rows. What's going on
here? Is there any better way to do this? It's driving me nuts.

Thanks
 
A

Alvin Bruney

go back to the autogenerated column. here is the code to hide it
you need to do this in the itemdatabound event handler
foreach(string toHide in ListBox1)

{

if(toHide != " ")

e.Item.Cells[int.Parse(toHide)].Visible = false;

}

toHide would be an index of the selected item instead of the string because
Cells only takes an int.

happy camping
 
H

headware x

Well, the DataGrid.Items property doesn't have a Cells property so I'm
not sure what you mean. Do you mean the DataGrid.Columns collection? If
so, it still doesn't contain anything by the time execution gets to the
ItemDateBound event, so it's pretty much the same situation as before.
By the way, I'm doing this in C#, not VB if that matters.

Thanks
 
A

Alvin Bruney

you need to do this in the itemdatabound event handler.
For every row that gets bound in a grid, the itemdatabound event fires which
implies it must have something to bind to. If you look at my posted code, I
said e.Items.Cells. e comes from the itemdatabound event handler - it's part
of the event args.
 
H

headware

Gotcha, thanks for the help!

Alvin Bruney said:
you need to do this in the itemdatabound event handler.
For every row that gets bound in a grid, the itemdatabound event fires which
implies it must have something to bind to. If you look at my posted code, I
said e.Items.Cells. e comes from the itemdatabound event handler - it's part
of the event args.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top