Remove AutoGenerated DataGrid Column??

G

Guest

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...ontrolsdatagridcolumncollectionclasstopic.asp)


Anyone have any ideas?

TIA,
Steve
 
E

Eliyahu Goldin

Steve,

The only chance to intercept an AutoGenerated column is ItemCreated event.
Try setting Visible=false in the event handler.

Eliyahu
 
G

Guest

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
 
G

Guest

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 said:
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 said:
Hi Steve,

You can have a datakeyfield specified on the datagrid that would not display
http://msdn.microsoft.com/library/d...ontrolsbasedatalistclassdatakeyfieldtopic.asp

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 
G

Guest

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 said:
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 said:
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 said:
Hi Steve,

You can have a datakeyfield specified on the datagrid that would not display
http://msdn.microsoft.com/library/d...ontrolsbasedatalistclassdatakeyfieldtopic.asp

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.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...ontrolsdatagridcolumncollectionclasstopic.asp)


Anyone have any ideas?

TIA,
Steve
 
G

Guest

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.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 said:
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 said:
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 said:
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


:

Hi Steve,

You can have a datakeyfield specified on the datagrid that would not display
http://msdn.microsoft.com/library/d...ontrolsbasedatalistclassdatakeyfieldtopic.asp

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.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...ontrolsdatagridcolumncollectionclasstopic.asp)


Anyone have any ideas?

TIA,
Steve
 
G

Guest

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...questionsaboutaspnetdatagridservercontrol.asp

Creating Web Server Control Templates Programmatically
http://msdn.microsoft.com/library/d...webservercontroltemplatesprogrammatically.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 );
}
}
 
G

Guest

Eliyahu,

Thanks for your input. See my last reply to Phillip for what I ended up
with as a final implementation.

Steve
 
G

Guest

You are welcome, Steve. I am glad you found a solution that worked in your
scenario. And thanks for sharing it.
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top