Programmatically Building an Item's Content

P

Phil Jones

I'm wondering if I can formulate the display 'content' of a data-grid item
from within a procedure in a code-behind class?
I can see that this can be done declaratively with something like this:

<ASP:DATAGRID ID="grdMembers" RUNAT="server">
<COLUMNS>
<ASP:TEMPLATECOLUMN>
<HeaderTemplate>Email</HeaderTemplate>
<ItemTemplate>
<ASP:LABEL RUNAT="server"
TEXT='<%# DataBinder.Eval(Container.DataItem,
"Email.Address.Value") %>' />
</ItemTemplate>
</ASP:TEMPLATECOLUMN>
</COLUMNS>
</ASP:DATAGRID>


But I'd like to have more control over the creation of the content than what
can be expressed witin the "DataBinder.Eval" statement. And I'd also like
to programmatically construct the columns within the DataGrid rather than
declare them.

Is this possible? Can anyone point me in the right direction here, perhaps
to some sample code somewhere.
Many thanks.....
 
J

Jeffrey Tan[MSFT]

Hi Phil,

Thank you for posting in the community!

Based on my understanding, you want to dynamicly add a column to the
datagrid and do the databinding formatting for this column.

==========================================
To dynamicly create a column, you should create a TemplateColumn and use
ITempalte to instance the TemplateColumn.ItemTemplate.

Then, to do databinding expression formatting, you should refer to
DataGrid.ItemDataBound event. In this event, you can use
DataGridItemEventArgs.Item.DataItem to get the DataRowView, then you can
refer to the datasource.

I have writen a sample for you in C#:

protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
getsource();
dodatabinding();
}
}

private DataSet ds;
private void getsource()
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from jobs",
"server=localhost;database=pubs;uid=sa;pwd=");
ds=new DataSet();
adapter.Fill(ds);
}

private void dodatabinding()
{
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

public class lablecolumn: ITemplate
{
public void InstantiateIn(Control container)
{
Label lb=new Label();
lb.ID="lb";
container.Controls.Add(lb);
}
}

private void WebForm1_Init(object sender, System.EventArgs e)
{
TemplateColumn tc=new TemplateColumn();
lablecolumn lbc=new lablecolumn();
tc.ItemTemplate=lbc;
DataGrid1.Columns.Add(tc);
}

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGridItem dgi=e.Item;

if(dgi.ItemType==ListItemType.Item||dgi.ItemType==ListItemType.AlternatingIt
em)
{
Label lb=dgi.FindControl("lb") as Label;
if(lb!=null)
{
Int16 jobid=Convert.ToInt16(((DataRowView)e.Item.DataItem)["job_id"]);
if(jobid>5)
{
lb.Text="Index >5";
}
else
{
lb.Text="Index <=5";
}
}
}
}

In the code, I use the "Jobs" table in SqlServer's default "pubs" database.
In DataGrid.ItemDataBound event, I judge the "job_id" field, if it is
greater than 5, then output "Index >5", else output "Index <=5".

Note: You should instance and add the new created column in Page.Init event.

Also, you can refer to an article of "Dino Esposito" talks about column at
below:
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Phil Jones

Cool....thanks Jeffrey that's what I was after. Much appreciated
===
Phil


""Jeffrey Tan[MSFT]"" said:
Hi Phil,

Thank you for posting in the community!

Based on my understanding, you want to dynamicly add a column to the
datagrid and do the databinding formatting for this column.

==========================================
To dynamicly create a column, you should create a TemplateColumn and use
ITempalte to instance the TemplateColumn.ItemTemplate.

Then, to do databinding expression formatting, you should refer to
DataGrid.ItemDataBound event. In this event, you can use
DataGridItemEventArgs.Item.DataItem to get the DataRowView, then you can
refer to the datasource.

I have writen a sample for you in C#:

protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
getsource();
dodatabinding();
}
}

private DataSet ds;
private void getsource()
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from jobs",
"server=localhost;database=pubs;uid=sa;pwd=");
ds=new DataSet();
adapter.Fill(ds);
}

private void dodatabinding()
{
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

public class lablecolumn: ITemplate
{
public void InstantiateIn(Control container)
{
Label lb=new Label();
lb.ID="lb";
container.Controls.Add(lb);
}
}

private void WebForm1_Init(object sender, System.EventArgs e)
{
TemplateColumn tc=new TemplateColumn();
lablecolumn lbc=new lablecolumn();
tc.ItemTemplate=lbc;
DataGrid1.Columns.Add(tc);
}

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGridItem dgi=e.Item;

if(dgi.ItemType==ListItemType.Item||dgi.ItemType==ListItemType.AlternatingIt
em)
{
Label lb=dgi.FindControl("lb") as Label;
if(lb!=null)
{
Int16 jobid=Convert.ToInt16(((DataRowView)e.Item.DataItem)["job_id"]);
if(jobid>5)
{
lb.Text="Index >5";
}
else
{
lb.Text="Index <=5";
}
}
}
}

In the code, I use the "Jobs" table in SqlServer's default "pubs" database.
In DataGrid.ItemDataBound event, I judge the "job_id" field, if it is
greater than 5, then output "Index >5", else output "Index <=5".

Note: You should instance and add the new created column in Page.Init event.

Also, you can refer to an article of "Dino Esposito" talks about column at
below:
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top