RE: Remove AutoGenerated DataGrid Column??



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:

> 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:
>
> > 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:
> >
> > > 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:
> > >
> > > > Hi Steve,
> > > >
> > > > You can have a datakeyfield specified on the datagrid that would not display
> > > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsbasedatalistclassdatakeyfieldtopic.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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridcolumncollectionclasstopic.asp)
> > > > >
> > > > >
> > > > > Anyone have any ideas?
> > > > >
> > > > > TIA,
> > > > > Steve
.