how to bind Datagrid?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ripendra007
    New Member
    • Feb 2007
    • 27

    #1

    how to bind Datagrid?

    hi everyone,
    i have my datagrid populated with my Dataset
    like
    DataGrid1.DataS ource=Dataset1;
    this data set contains the value of count the total of email sent ,receive and deliver for a particular user id now i want to display user id with taht data but i dont have user name in my dataset because i can not write query like

    select uname,count(sen d_email) from table where uname="ABCD";

    because here i used aggregate function
    ...here i m selecting the user name from listbox so the query will saw the count of send ,receive and etc information for that user.now i want to display the user name in Datagrid that i selected from listbox so how can i add that valu or column to datgrid
    Regards........ ...........
    Ripen
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    Originally posted by Ripendra007
    hi everyone,
    i have my datagrid populated with my Dataset
    like
    DataGrid1.DataS ource=Dataset1;
    this data set contains the value of count the total of email sent ,receive and deliver for a particular user id now i want to display user id with taht data but i dont have user name in my dataset because i can not write query like

    select uname,count(sen d_email) from table where uname="ABCD";

    because here i used aggregate function
    ...here i m selecting the user name from listbox so the query will saw the count of send ,receive and etc information for that user.now i want to display the user name in Datagrid that i selected from listbox so how can i add that valu or column to datgrid
    Regards........ ...........
    Ripen
    As i understand from your explaination you want to add the username to all of your rows. This can be done with the OnItemDataBound . I assume you add a new column to your datagrid and you use a literal with the id: username. The OnItemDataBound attribute is a part of the asp:Datagrid tag and the value is a function, lets say dataBound.

    Code:
    <%@ Page Language="C#"  %>
    
    <script runat="server">
    void dataBound(Object sender, DataGridItemEventArgs e)
        {
            // get bound data + database connection
            DataRowView row = (DataRowView)e.Item.DataItem;
            OdbcConnection conn = new OdbcConnection(ConfigurationSettings.AppSettings["connString"]);
            // for edit templates
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                // if it's the same code you best put it another function and call that.
                string sql = "select uname,count(send_email) from table where uname=?";
                OdbcCommand com = new OdbcCommand(sql, conn);
                com.Parameters.Add("", OdbcType.Int, 10).Value = [B]Convert.ToInt32(row["id"]);[/B]
                // and so on
           }
            // for other templates
            else if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
             
             }
    }
    </script>
    <ASP:DataGrid id="DataGrid1" runat="server" 
            OnItemDataBound="dataBound"
            AutoGenerateColumns="false"
          >
    <!-- other columns -->
    <asp:TemplateColumn HeaderText="Username">
                    <ItemTemplate><asp:Literal id="username" runat="server" /></ItemTemplate>
                    <EditItemTemplate>
                        <h3>Ingeschreven</h3>
                        <asp:Literal id="usernameedit" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateColumn>
    <!-- other columns -->
    Sorry i haven't much time now but i think you can get the intention.

    Comment

    Working...