Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Programmatically Building an Item's Content

Reply
Thread Tools

Programmatically Building an Item's Content

 
 
Phil Jones
Guest
Posts: n/a
 
      02-27-2004
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:

<ASPATAGRID 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>
</ASPATAGRID>


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.....

--
Phil
(Auckland | Aotearoa)


 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      02-27-2004

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.DataIte m)["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/is...g/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.

 
Reply With Quote
 
 
 
 
Phil Jones
Guest
Posts: n/a
 
      03-01-2004
Cool....thanks Jeffrey that's what I was after. Much appreciated
===
Phil


"""Jeffrey Tan[MSFT]""" <(E-Mail Removed)> wrote in message
news:KzMARMQ$(E-Mail Removed)...
>
> 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.DataIte m)["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/is...g/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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
any JSP tags for programmatically building diagrams/graphics? davout Java 0 07-03-2006 09:46 PM
Help - Programmatically Building a Treeview from Database Teemu ASP .Net Web Controls 0 04-01-2004 11:37 PM
Help - Programmatically Building a Treeview from Database Teemu ASP .Net 0 04-01-2004 11:36 PM
change web form content type programmatically andy ASP .Net 1 07-15-2003 04:53 AM



Advertisments