Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FAQ - Adding template columns to a grid at run time 2

Status
Not open for further replies.

JCruz063

Programmer
Feb 21, 2003
716
US
Hey guys,
A while ago, I posted an FAQ about adding template columns to a datagrid at run time. Whenever you have a chance (and if you're interested), take a look at it - faq855-4868. I think it might be helpful.

Thanks!

BTW: Three people have already emailed me about the FAQ, yet I haven't seen any of them in any of the tek-tips forums. They refer to the FAQ as "the article that you wrote...". I wonder if FAQs are seen by other people outside the boundaries of tek-tips... just wondering!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Nice contribution JC. I downloaded it and will take a look. I'm sure there are many who come by Tek-Tips and take advantage of the many great posts here.
 
how can i set the selected value of a dropdownlist that is added at runtime thru your templatecolumn technique??

keep getting object not referenced errors..
grrrr!
 
aftertaf,
We'll be glad to help you! Just post post your code so that we could see what's going on...

Thanks!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
hi all,
i've got around the referencing the dropdownlist problem to some extent... the selected index property is available, so i enumerate my datatable comparing the primary keys and set the index appropriately.

now the pb is recovering the selected value by the user during an update in order to send it as a parameter to my Stored procedure....

i can mail my code, but it's a bit of a mess :)
________________________
class DDLTemplate : System.Web.UI.ITemplate {
string strParent;
DataView dataparent;
int RowToEdit;

public DDLTemplate(string strParentbinding, DataView dView, int rowEditing){
this.strParent = strParentbinding;
this.dataparent = dView;
this.RowToEdit = rowEditing;}

public void InstantiateIn (System.Web.UI.Control container){
DropDownList ddl = new DropDownList();
ddl.DataValueField="id"+ strParent;
ddl.DataTextField="lib"+ strParent;
ddl.DataSource = dataparent;
ddl.ID="ddlparent";//this ID is not implemented in run time :(

ddl.DataBinding += new System.EventHandler(this.BindDDL);
container.Controls.Add(ddl);

//depending on row neing edited, show the ddl or show the label :)
Label lbl = new Label();
lbl.ID="lblparent";
lbl.DataBinding += new System.EventHandler(this.BindLbl);
container.Controls.Add(lbl);}

private void BindDDL(object sender, System.EventArgs e){
DropDownList ddl = (DropDownList)sender;
DataGridItem container = (DataGridItem)ddl.NamingContainer;
//use itemindex property of container to know whether this line is in edition or read only...
//and hide or show control accordingly
if(container.DataSetIndex== this.RowToEdit) {
ddl.Visible=true;
ddl.SelectedIndex= GetDBindID(DataBinder.Eval(container.DataItem,GetDBinding("id").ToString()).ToString());}
else
//no binding!
ddl.Visible=false;}

private void BindLbl(object sender, System.EventArgs e){
Label lbl = (Label)sender;
DataGridItem container = (DataGridItem)lbl.NamingContainer;

//use itemindex property of container to know whether this line is in edition or read only...
//and hide or show control accordingly
if(container.DataSetIndex!= this.RowToEdit) {
lbl.Visible=true;
//bind data to ddl
lbl.Text=GetDBindLib(DataBinder.Eval(container.DataItem,GetDBinding("id").ToString()).ToString());}
else {
//no label binding!
lbl.Visible=false;}}

private string GetDBinding(string prefix) {
return prefix + strParent; }

private string GetDBindLib(string data){
IEnumerator Myenum = dataparent.GetEnumerator();
//need to enumerate data to find libellé for identifiant for each row...
while (Myenum.MoveNext()) {
DataRowView tmpRow = (DataRowView)Myenum.Current;
string sInt= tmpRow.Row[0].ToString();
string sLib = tmpRow.Row[1].ToString();
if (sInt == data) return sLib;
}
return "vide";
}

private int GetDBindID(string libellé){
int intindex = -1;
IEnumerator Myenum = dataparent.GetEnumerator();
//need to enumerate data to find libellé for identifiant for each row...
while (Myenum.MoveNext()) {
intindex+=1;
DataRowView tmpRow = (DataRowView)Myenum.Current;
string sInt= tmpRow.Row[0].ToString();
string str = tmpRow.Row[1].ToString();
if (sInt == libellé)return intindex;}
return intindex;
}
}
________________________

and the code calling the class:
_______
private void DoGridGestion(){
this.dgridGestion.Visible=true;
string element = Session["GererElement"].ToString() + "s";
switch (element){
case "Structures": //affichage directe datagrid avec ttes les structures
this.dgridGestion.DataSource = dsDatasetAntennes.Tables[element];
break;
default:
string ddlValue = this.ddlAppartenance.SelectedItem.Value;
/* passage au code pour prendre en compte choix du DDL pour eventuel filtrage avant d'afficher le contenu du datagrid*/
string parentID =recoverParentField(Session["GererElement"].ToString());
//apply filter to show only users choice members
DataView srcFiltered = new DataView(dsDatasetAntennes.Tables[element]); //view containing elements for the grid
if(ddlValue!= "-1") srcFiltered.RowFilter= "id" + parentID + " = '"+ ddlValue +"'";

//use of class begins here
//add template for ddl
TemplateColumn tc = new TemplateColumn();
tc.HeaderText = parentID;
switch(this.dgridGestion.EditItemIndex){
case -1: //mode lecture
tc.ItemTemplate = new labelTemplate(parentID, dsDatasetAntennes.Tables[parentID + "s"].DefaultView);
break;
default: //mode edition
tc.ItemTemplate = new DDLTemplate(parentID, dsDatasetAntennes.Tables[parentID + "s"].DefaultView, this.dgridGestion.EditItemIndex);
//dataview containing dependance/parent elements.
break;
}
this.dgridGestion.Columns.AddAt(3,tc);
this.dgridGestion.DataSource = srcFiltered;//apply filtered dataview as source for datagrid
break;
}

this.dgridGestion.DataBind();
}
_______

thx in advance....
david
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top