DataGrid (web) question

  • Thread starter Thread starter Tim Mulholland
  • Start date Start date
T

Tim Mulholland

(sorry if this is a repost - it never showed up in my newsreader)

I'm not too well-versed on how the web datagrid works when you're both
automatically generating columns, and setting some of them to be there
manually. But here's my issue, maybe you can tell me how to solve it.

I've got a datagrid displaying the results of an advanced search by a user.
The user can choose which columns he/she wants to be returned in the result
set, but there are some that are ALWAYS returned, and i don't want those to
be displayed by the datagrid (these are mostly the primary keys for the
tables being searched).

If i leave the datagrid to generate the columns automatically and then also
set it to have a bound and invisible column linked to one of the columns i
want to hide, it creates it twice, once for the bound column (which is
invisible) and once automatically (which is visible).

I basically need this Primary Key information so i can create the
appropriate links in some button columns i'm also adding to the datagrid.

Is there a way to do what i'm doing?

Am i going about it completely the wrong way?

Thanks in advance!

Tim
 
Your original post did show up in the ASP.NET discussion groups. I did put my suggestions there but I've copied it here again

In order to avoid the automatic creation of the columns set AutoGenerateColumns property to the DataGrid control to "False"

To access a row from the grid by it's key value you can set the DataKeyField property of the Datagrid

http://msdn.microsoft.com/library/d...ControlsBaseDataListClassDataKeyFieldTopic.as

Suresh


----- Tim Mulholland wrote: ----

(sorry if this is a repost - it never showed up in my newsreader

I'm not too well-versed on how the web datagrid works when you're bot
automatically generating columns, and setting some of them to be ther
manually. But here's my issue, maybe you can tell me how to solve it

I've got a datagrid displaying the results of an advanced search by a user
The user can choose which columns he/she wants to be returned in the resul
set, but there are some that are ALWAYS returned, and i don't want those t
be displayed by the datagrid (these are mostly the primary keys for th
tables being searched)

If i leave the datagrid to generate the columns automatically and then als
set it to have a bound and invisible column linked to one of the columns
want to hide, it creates it twice, once for the bound column (which i
invisible) and once automatically (which is visible)

I basically need this Primary Key information so i can create th
appropriate links in some button columns i'm also adding to the datagrid

Is there a way to do what i'm doing

Am i going about it completely the wrong way

Thanks in advance

Ti
 
Sorry for the double posting then... it still hasn't showed up to me, so i
thought it might have gotten lost in never-never-land.

I guess i sort of want a combination of automatic creation and manual
creation. I dont know in advance what fields will be in the datasource, so i
can't just set them at design time. I do however know that there will be one
field in there that i don't want to show, i just need it in the datasource
because its the key.

Is there a way to say "autogenerate all of these /except/ this one"? Or
should i be looking to add the data columns programmatically before
databinding? (you can do that right?)

Thanks for your help so far,

Tim

Suresh said:
Your original post did show up in the ASP.NET discussion groups. I did
put my suggestions there but I've copied it here again.
In order to avoid the automatic creation of the columns set
AutoGenerateColumns property to the DataGrid control to "False".
To access a row from the grid by it's key value you can set the
DataKeyField property of the Datagrid.
 
I've just gone the route of stepping through the collection of columns in
the datasource and creating the columns automatically that way, leaving out
the ones i know i want hidden, etc.
Its not as generic as i would have liked i suppose, but its doing the job
beautifully.

Thanks for your help,

Tim
 
Hi Tim,

I posted a response earlier, but I think it's lost in the ether somewhere. You can do it the way you're doing, by generating the columns automatically, but you can also do it with AutoGenerateColumns = true. In that case, in the handler for the ItemDataBound event, set visible=false for the cells you want to hide. ItemDataBound gets called once for every row added to the grid (including for headers, footers, etc.), so in effect each time the framework wants to generate HTML for that one column you're telling it not to. If the column you want to hide is the first in the dataset, you'd go: e.item.cells(0).visible = false. Just for kicks you might at least play with this, because it's a very powerful technique for altering the display at runtime, like if you want to display all currency values greater than a zillion dollars in a special font, etc.

Bill Borg

P.S. Normally I wouldn't use the ordinal position for anything, as this tends to break over time as things move around, and instead would use e.item.FindControl on the ID. In this case, though, I don't know that the ID that's autogenerated for your columns is going to mean anything to you.
 
good thought!
i'll look into that more in abit.

thanks!

Tim

Bill Borg said:
Hi Tim,

I posted a response earlier, but I think it's lost in the ether somewhere.
You can do it the way you're doing, by generating the columns automatically,
but you can also do it with AutoGenerateColumns = true. In that case, in
the handler for the ItemDataBound event, set visible=false for the cells you
want to hide. ItemDataBound gets called once for every row added to the
grid (including for headers, footers, etc.), so in effect each time the
framework wants to generate HTML for that one column you're telling it not
to. If the column you want to hide is the first in the dataset, you'd go:
e.item.cells(0).visible = false. Just for kicks you might at least play
with this, because it's a very powerful technique for altering the display
at runtime, like if you want to display all currency values greater than a
zillion dollars in a special font, etc.
Bill Borg

P.S. Normally I wouldn't use the ordinal position for anything, as this
tends to break over time as things move around, and instead would use
e.item.FindControl on the ID. In this case, though, I don't know that the
ID that's autogenerated for your columns is going to mean anything to you.
 
Back
Top