Home > Archive > ASP .NET > December 2005 > Remove AutoGenerated DataGrid Column??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Remove AutoGenerated DataGrid Column??
|
|
|
| How I can remove an AutoGenerated column? I wnat to inlcude the primary key
in the resultset for creating some custom LinkButtons, but I don't want it
(the PK) displayed in the DataGrid. I tried searching the columnheader text,
but found that AutoGenerated columns are members of the Columns collection.
From the msdn documentation:
Note: When the AutoGenerateColumns property is set to true, the columns
created by the DataGrid control are not added to the Columns collection.
(http://msdn.microsoft.com/library/d...nclasstopic.asp)
Anyone have any ideas?
TIA,
Steve
| |
| Eliyahu Goldin 2005-12-15, 3:58 am |
| Steve,
The only chance to intercept an AutoGenerated column is ItemCreated event.
Try setting Visible=false in the event handler.
Eliyahu
"Steve" <Steve@discussions.microsoft.com> wrote in message
news:8CB7A2AA-1D0C-4BCB-B800-982E80C4FDDB@microsoft.com...
> How I can remove an AutoGenerated column? I wnat to inlcude the primary
> key
> in the resultset for creating some custom LinkButtons, but I don't want it
> (the PK) displayed in the DataGrid. I tried searching the columnheader
> text,
> but found that AutoGenerated columns are members of the Columns
> collection.
> From the msdn documentation:
> Note: When the AutoGenerateColumns property is set to true, the columns
> created by the DataGrid control are not added to the Columns collection.
> (http://msdn.microsoft.com/library/d...nclasstopic.asp)
>
>
> Anyone have any ideas?
>
> TIA,
> Steve
| |
| Phillip Williams 2005-12-15, 3:58 am |
| Hi Steve,
You can have a datakeyfield specified on the datagrid that would not display
http://msdn.microsoft.com/library/d...yfieldtopic.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve" wrote:
> How I can remove an AutoGenerated column? I wnat to inlcude the primary key
> in the resultset for creating some custom LinkButtons, but I don't want it
> (the PK) displayed in the DataGrid. I tried searching the columnheader text,
> but found that AutoGenerated columns are members of the Columns collection.
> From the msdn documentation:
> Note: When the AutoGenerateColumns property is set to true, the columns
> created by the DataGrid control are not added to the Columns collection.
> (http://msdn.microsoft.com/library/d...nclasstopic.asp)
>
>
> Anyone have any ideas?
>
> TIA,
> Steve
| |
|
| Thanks for the suggestion Phillip. I hate to sound like an idiot, but I
can't seem to make that work. I tried the example code exactly as is (cut &
paste) and it didn't work either (their "IntegerValue" pk field /did/
display). I also tried the following, to no avail:
DataColumn[] keys = new DataColumn[1];
keys[0] = ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = keys;
keys[0].ColumnMapping = MappingType.Hidden; //added this
DataView dv = new DataView( ds.Tables[0] );
dg.DataKeyField = "itemPk";
dg.DataSource = dv;
dg.DataBind();
Am I missing something obvious?
Thanks,
Steve
"Phillip Williams" wrote:
[color=darkred]
> Hi Steve,
>
> You can have a datakeyfield specified on the datagrid that would not display
> http://msdn.microsoft.com/library/d...yfieldtopic.asp
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Steve" wrote:
>
| |
| Phillip Williams 2005-12-15, 7:02 pm |
| Hi Steve,
Actually you are not. It was my mistake. The link I gave was meant as a
reference to the DataKeyField property. But the example there would show
all of the fields on the dataset (because it has AutoGenerateColumns= true).
The proper way to select certain columns is to turn off the
AutoGenerateColumns and list the fields specifically using <asp:BoundColumn>
as I did in this demo: http://www.societopia.net/samples/dataGrid_3c.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve" wrote:
[color=darkred]
> Thanks for the suggestion Phillip. I hate to sound like an idiot, but I
> can't seem to make that work. I tried the example code exactly as is (cut &
> paste) and it didn't work either (their "IntegerValue" pk field /did/
> display). I also tried the following, to no avail:
>
> DataColumn[] keys = new DataColumn[1];
> keys[0] = ds.Tables[0].Columns[0];
> ds.Tables[0].PrimaryKey = keys;
> keys[0].ColumnMapping = MappingType.Hidden; //added this
> DataView dv = new DataView( ds.Tables[0] );
>
> dg.DataKeyField = "itemPk";
> dg.DataSource = dv;
> dg.DataBind();
>
> Am I missing something obvious?
>
> Thanks,
> Steve
>
>
> "Phillip Williams" wrote:
>
| |
|
| Thanks Phillip, no problem. I'm familiar with the column definition paradigm
in the DataGrid. My issue is that I allow column naming and selection via
the user interface, so by the time the data gets to the datagrid, I have an
unknown number of columns, whose names I also don't know. The only column I
retain is the primary key. This scenario is an easy fit autogenerating the
columns, and not so great for templating (unless you know something I don't
know). I actually pondered the idea of generating a template in an .ascx
file and then loading it from disk, but I haven't tried it yet (seems hokey).
I instead opted for a simple manual nested row/column table generation loop.
A little old-school maybe, but effective.
If you have any suggestions, I'm open for options.
Thanks again,
Steve
"Phillip Williams" wrote:
[color=darkred]
> Hi Steve,
>
> Actually you are not. It was my mistake. The link I gave was meant as a
> reference to the DataKeyField property. But the example there would show
> all of the fields on the dataset (because it has AutoGenerateColumns= true).
> The proper way to select certain columns is to turn off the
> AutoGenerateColumns and list the fields specifically using <asp:BoundColumn>
> as I did in this demo: http://www.societopia.net/samples/dataGrid_3c.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Steve" wrote:
>
| |
| Phillip Williams 2005-12-15, 7:02 pm |
| Hi Steve,
If the only column you are certain of is the Primary Key then try Eliyahu's
suggestion, namely setting the column visible property to false in the
ItemCreated event, e.g.
private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
int iColNum=-1;
//if you know which column will have the Primarkey then you can write
iColNum =0;
e.Item.Cells[iColNum].Visible=false;
//if you do not know which column number you might have to
add some
//code that looks up the columns within the dataitem
DataRowView drv = (DataRowView)e.Item.DataItem;
for(int i=0;i< drv.DataView.Table.Columns.Count ;i++)
{
if (drv.DataView.Table.Columns[i].ColumnName =="Person_ID")
{
iColNum =i;
//if you have added other columns such as
button commands then
//increment that number accordingly
break;
}
}
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve" wrote:
[color=darkred]
> Thanks Phillip, no problem. I'm familiar with the column definition paradigm
> in the DataGrid. My issue is that I allow column naming and selection via
> the user interface, so by the time the data gets to the datagrid, I have an
> unknown number of columns, whose names I also don't know. The only column I
> retain is the primary key. This scenario is an easy fit autogenerating the
> columns, and not so great for templating (unless you know something I don't
> know). I actually pondered the idea of generating a template in an .ascx
> file and then loading it from disk, but I haven't tried it yet (seems hokey).
> I instead opted for a simple manual nested row/column table generation loop.
> A little old-school maybe, but effective.
>
> If you have any suggestions, I'm open for options.
>
> Thanks again,
> Steve
>
> "Phillip Williams" wrote:
>
| |
|
| Phillip and Eliyahu,
I don't know if either of you are still checking this thread, but I wanted
to thank you both for your help and suggestions, and also that I found a good
solution that's a little better than catching the ItemCreated event.
First, removing the cells (Visible = false) during ItemCreated worked,
except during a PostBack they came back. So, I switched to Cells.RemoveAt( 0
) (first column), which worked a little better since they stayed gone during
a PostBack, but... the ColumnHeader text reappeared :(. I didn't really do
investigation beyond that because I wans't totally in love with handling this
at the row level anyway. Pressing on, I tripped across the following two
articles which answered my questions perfectly:
Top Questions about the DataGrid Web Server Control
http://msdn.microsoft.com/library/d...>
ercontrol.asp
Creating Web Server Control Templates Programmatically
http://msdn.microsoft.com/library/d...ammatically.asp
In the first article, they explain how to create BoundColumns
programmatically, which I don't know why I never though of in the first
place. My implementation ended up as follows below. This works great
because I don't have to anything special at the row level and I can retain
all the standard datagrid functionality, such as sorting and paginating.
Again, thank you both for your input and advice.
Steve
private void dgItems_DataBinding(object sender, System.EventArgs e)
{
//c=1 indicates to skip column 0, which is the PK column
DataTable dt = (DataTable)dgItems.DataSource;
for( int c=1; c<dt.Columns.Count; c++ )
{
BoundColumn bc = new BoundColumn();
bc.DataField = dt.Columns[c].ColumnName;
bc.HeaderText = dt.Columns[c].ColumnName;
dgItems.Columns.Add( bc );
}
}
| |
|
| Eliyahu,
Thanks for your input. See my last reply to Phillip for what I ended up
with as a final implementation.
Steve
"Eliyahu Goldin" wrote:
> Steve,
>
> The only chance to intercept an AutoGenerated column is ItemCreated event.
> Try setting Visible=false in the event handler.
>
> Eliyahu
>
> "Steve" <Steve@discussions.microsoft.com> wrote in message
> news:8CB7A2AA-1D0C-4BCB-B800-982E80C4FDDB@microsoft.com...
>
>
>
| |
| Phillip Williams 2005-12-16, 9:58 pm |
| You are welcome, Steve. I am glad you found a solution that worked in your
scenario. And thanks for sharing it.
--
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Steve" wrote:
> Phillip and Eliyahu,
>
> I don't know if either of you are still checking this thread, but I wanted
> to thank you both for your help and suggestions, and also that I found a good
> solution that's a little better than catching the ItemCreated event.
>
> First, removing the cells (Visible = false) during ItemCreated worked,
> except during a PostBack they came back. So, I switched to Cells.RemoveAt( 0
> ) (first column), which worked a little better since they stayed gone during
> a PostBack, but... the ColumnHeader text reappeared :(. I didn't really do
> investigation beyond that because I wans't totally in love with handling this
> at the row level anyway. Pressing on, I tripped across the following two
> articles which answered my questions perfectly:
>
> Top Questions about the DataGrid Web Server Control
> http://msdn.microsoft.com/library/d...>
ercontrol.asp
>
> Creating Web Server Control Templates Programmatically
> http://msdn.microsoft.com/library/d...ammatically.asp
>
> In the first article, they explain how to create BoundColumns
> programmatically, which I don't know why I never though of in the first
> place. My implementation ended up as follows below. This works great
> because I don't have to anything special at the row level and I can retain
> all the standard datagrid functionality, such as sorting and paginating.
>
> Again, thank you both for your input and advice.
>
> Steve
>
>
> private void dgItems_DataBinding(object sender, System.EventArgs e)
> {
> //c=1 indicates to skip column 0, which is the PK column
> DataTable dt = (DataTable)dgItems.DataSource;
> for( int c=1; c<dt.Columns.Count; c++ )
> {
> BoundColumn bc = new BoundColumn();
> bc.DataField = dt.Columns[c].ColumnName;
> bc.HeaderText = dt.Columns[c].ColumnName;
> dgItems.Columns.Add( bc );
> }
> }
>
|
|
|
|
|