RE: Remove AutoGenerated DataGrid Column??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Help with Hurricane Database
    ... Steve is the problem and you were a potential ... victim. ... relationship between the Hurricane table and the TblUnitForm. ... That's exactly what I said; add UnitID as the primary key - it's the ...
    (microsoft.public.access.formscoding)
  • Re: Generate answer list for book of multiple-choice questions?
    ... > You will need a macro to make your answer table. ... > suggestion to hide the answer, and you use list styles to number the ... > Steve Hudson - Word Heretic ... >>> Dim Stem as string ...
    (microsoft.public.word.vba.general)
  • Re: Recording Date and Time--More Info Needed
    ... That post contains my response to your suggestion which I won't repeat ... "Steve Schapel" wrote: ... >> Earlier today I submitted the question, "In an Access form set up to allow ... This event triggers when the first ...
    (microsoft.public.access.gettingstarted)
  • Re: How to counting words in a string variable?
    ... sorry nad thanks for your suggestion. ... Steve Rindsberg, PPT MVP ... PPTools: www.pptools.com ...
    (microsoft.public.powerpoint)
  • Re: Why does 1:3 relationsihp require another table?
    ... I have to agree with Steve here, did you really mean that the user will have ... a user and a password have a 90-day time frame constraint. ... >> start_date DATETIME NOT NULL, ... >> PRIMARY KEY, ...
    (microsoft.public.sqlserver.programming)