Dynamic control creation in datagrid based on data in that column

M

mitramay

I am creating a dynamic datagrid. The controls in some of the template
columns will have to be generated based on the data contained in them
(i.e. the data for that respective DataField column). For example, if I

have a column called Available, the possible values for it are Yes or
No. In case it is "Yes" I need a label in that column and in case it is

"No" I need a hyperlink. Pls help.
 
B

Baski

add event handler to your datagrid ItemDatabound event as below

private void dg_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

// get your data using your datasource for the selected index( ie,
e.Item.ItemIndex)

bool bYes = false; //Check for yes or no value here

if(bYes)

{


Label lbl = new Label();

lbl.Text = "Yes";

e.Item.Cells[0].Controls.Add(lbl); // I assume your template column is first
column , if not adjust your cell index accordingly

}

else

{

HyperLink link = new HyperLink ();

link .Text= "No";

link.NavigateUrl = "";/* set your url here */

e.Item.Cells[0].Controls.Add(link);

}

}

}
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top