How to fill a datagrid templated label from db

Last post 07-05-2009 4:30 PM by blackbr. 2 replies.

Sort Posts:

  • How to fill a datagrid templated label from db

    07-04-2009, 5:22 PM
    • Member
      32 point Member
    • blackbr
    • Member since 11-15-2006, 6:35 PM
    • Posts 38

    I have a datagrid bound to a datasource.  For each item in the grid, I want to compute the value of a templated label field, based on other (non-grid) data  in the db record.

    What is the best way to do this?


    Thanx,

    Randy

  • Re: How to fill a datagrid templated label from db

    07-04-2009, 7:36 PM
    Answer
    • All-Star
      25,434 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,620

    you can accommodate that in your query (select statement) itself, if it is simple?

    else you could change for each item while it is getting bound in the ItemDataBound event but first you'll have to wire up the ItemDataBound event, for example:

    <asp:DataGrid id="ItemsGrid" runat="server"
    BorderColor="black"
    BorderWidth="1"
    CellPadding="3"
    ShowFooter="true"
    OnItemDataBound="Item_Bound"
    AutoGenerateColumns="true">

    codebehind:

    protected void Item_Bound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) {

            DataRowView drv = (System.Data.DataRowView)e.Item.DataItem;       

            Label lbl = (Label)e.Item.FindControl("Label1");

            if (lbl != null)
            {
                //get data from DataItem and proceed:
                lbl.Text =
    drv["data"].ToString();
            }       
       }
    }

    Regards,
    Peter
  • Re: How to fill a datagrid templated label from db

    07-05-2009, 4:30 PM
    • Member
      32 point Member
    • blackbr
    • Member since 11-15-2006, 6:35 PM
    • Posts 38

    Peter,


    That was just what I needed.... thanks


    Randy

Page 1 of 1 (3 items)
Microsoft Communities