datagrid question

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

Tim Mulholland

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
 
In order to avoid the design time creation of the columns set AutoGenerateColumns property to the DataGrid control to "False"

should b

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


----- Suresh wrote: ----

In order to avoid the design time 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: ----

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
 
weird, all of this showed up here, just not when i was at work.
oh well.
thanks to all of you for helping out in the other thread just above this
one!


Bill Borg said:
Hi Tim,

Per Suresh's note, AutoGenerateColumns is an all-or-nothing thing. If
it's on, you get everthing, and if it's off you have to do a bunch of work
manually. I can see two options for you.
1. AutoGenerateColumns = true. You'll get all the columns, including the
hidden ones, so for these you need to set visible=false. You'd do this in
the ItemDataBound event for the grid. This event is basically your last
chance to alter the controls/data before they'll go to the screen.
ItemDataBound happens once as each dataitem (a row on the grid) is added, so
you'll get it once for each row in your grid. In that event, set
visible=false for the column(s) you want to hide. Here's a snippet:
Private Sub grdData_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles grdData.ItemDataBound

e.Item.Cells(0).Visible = False (this presumes you are hiding the first column, e.g. Cells(0))

End Sub

Something else to watch out for: ItemDataBound happens for *every* row
that is going to show up on the screen, such as the header, footer, pager,
etc. In your ItemDataBound, you can trap for these by looking at
e.item.itemtype, which has an enumeration of the different types you are
dealing with. For a "normal" data row, you want to trap for both Item and
Alternating item, in the pretty typical case of wanting to do something with
just the data and not the headings, etc.
2. AutoGenerateColumns = false. You'll have to build the columns yourself
in code, since the user gets to pick his own. This isn't too tough, as long
as you make sure you set DataField on the column to match the column name of
your dataset.
In my experience the first way is a little harder to figure out the first
time around, but it's very powerful and something you'll use a lot down the
road.
 
Back
Top