By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

how to bind Datagrid?

P: 27
hi everyone,
i have my datagrid populated with my Dataset
like
DataGrid1.DataSource=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(send_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
Feb 26 '07 #1
Share this Question
Share on Google+
1 Reply


P: 99
hi everyone,
i have my datagrid populated with my Dataset
like
DataGrid1.DataSource=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(send_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.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#"  %>
  2.  
  3. <script runat="server">
  4. void dataBound(Object sender, DataGridItemEventArgs e)
  5.     {
  6.         // get bound data + database connection
  7.         DataRowView row = (DataRowView)e.Item.DataItem;
  8.         OdbcConnection conn = new OdbcConnection(ConfigurationSettings.AppSettings["connString"]);
  9.         // for edit templates
  10.         if (e.Item.ItemType == ListItemType.EditItem)
  11.         {
  12.             // if it's the same code you best put it another function and call that.
  13.             string sql = "select uname,count(send_email) from table where uname=?";
  14.             OdbcCommand com = new OdbcCommand(sql, conn);
  15.             com.Parameters.Add("", OdbcType.Int, 10).Value = Convert.ToInt32(row["id"]);
  16.             // and so on
  17.        }
  18.         // for other templates
  19.         else if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
  20.         {
  21.  
  22.          }
  23. }
  24. </script>
  25. <ASP:DataGrid id="DataGrid1" runat="server" 
  26.         OnItemDataBound="dataBound"
  27.         AutoGenerateColumns="false"
  28.       >
  29. <!-- other columns -->
  30. <asp:TemplateColumn HeaderText="Username">
  31.                 <ItemTemplate><asp:Literal id="username" runat="server" /></ItemTemplate>
  32.                 <EditItemTemplate>
  33.                     <h3>Ingeschreven</h3>
  34.                     <asp:Literal id="usernameedit" runat="server" />
  35.                 </EditItemTemplate>
  36.             </asp:TemplateColumn>
  37. <!-- other columns -->
  38.  
Sorry i haven't much time now but i think you can get the intention.
Feb 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.