Programmatically Building an Item's Content

Discussion in 'ASP .Net Datagrid Control' started by Phil Jones, Feb 27, 2004.

  1. Phil Jones

    Phil Jones Guest

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

    --
    Phil
    (Auckland | Aotearoa)
     
    Phil Jones, Feb 27, 2004
    #1
    1. Advertising

  2. 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.
     
    Jeffrey Tan[MSFT], Feb 27, 2004
    #2
    1. Advertising

  3. Phil Jones

    Phil Jones Guest

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


    """Jeffrey Tan[MSFT]""" <> wrote in message
    news:KzMARMQ$...
    >
    > 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.
    >
     
    Phil Jones, Mar 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Criswell
    Replies:
    1
    Views:
    1,402
    John Criswell
    Jul 24, 2003
  2. Teemu
    Replies:
    0
    Views:
    452
    Teemu
    Apr 2, 2004
  3. hazz
    Replies:
    6
    Views:
    49,954
    SkyUCHC
    Jun 9, 2010
  4. davout
    Replies:
    0
    Views:
    347
    davout
    Jul 3, 2006
  5. Teemu
    Replies:
    0
    Views:
    96
    Teemu
    Apr 2, 2004
Loading...

Share This Page