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

Problem in nested gridview

100+
P: 375
Hi,

I have a gridview problem while binding more than two gridviews.

I am giving a sample scenario of what I have done so far

I have two tables

First table: Department , having columns DeptId and DeptName

10 Accounts
20 Software

Second Table: Emp having columns EmpId, EmpName,Salary,
DeptID.

101 aaa 100 10
101 aaa 200 10
101 aaa 300 10
202 bb 101 20
202 bb 102 20
102 cc 100 10
201 dd 103 20


I am supposed to show first all the employees ordered by their deptname.

Also I am supposed to show the sum of each employees salary and sum of the entire department's salary

So the above values should appear as below

101 aaa 100 10
101 aaa 200 10
101 aaa 300 10
sum of emp 101 is 600
102 cc 100 10
sum of emp 102 is 100
SUM OF DEPT 10 IS 700


202 bb 101 20
202 bb 102 20
sum of emp 202 is 203
201 dd 103 20
sum of emp 202 is 103
SUM OF DEPT 20 IS 306


I am able to fetch sum of dept salary but not of employees

I have tried as below

My Design Page
.


Expand|Select|Wrap|Line Numbers
  1.  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
  2.                 <Columns>
  3.                 <asp:BoundField DataField="DeptID" />
  4.                     <asp:TemplateField>
  5.                         <ItemTemplate>
  6.  
  7.                                 <asp:GridView ID="GridView2"  AutoGenerateColumns="false" runat="server">
  8.                                     <Columns>
  9.                                         <asp:BoundField DataField="EmpId" />
  10.                                         <asp:BoundField DataField="EmpName" />
  11.                                         <asp:BoundField DataField="Salary" />
  12.                                     </Columns>
  13.                                 </asp:GridView>
  14.                                 <asp:GridView ID="GridView3"  AutoGenerateColumns="false" runat="server">
  15.                                     <Columns>
  16.                                         <asp:BoundField DataField="SumSalary" />
  17.                                     </Columns>
  18.                                 </asp:GridView>
  19.  
  20.                         </ItemTemplate>
  21.                     </asp:TemplateField>
  22.                 </Columns>
  23.             </asp:GridView>
My Codebehind page is as below


Expand|Select|Wrap|Line Numbers
  1.  SqlConnection MyCon=new SqlConnection("server=ABC-415D0247602\\SQLEXPRESS; database=Employee ;integrated security=true;connection timeout=600");
  2.  
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         MyCon.Open();
  6.         SqlDataAdapter MyDa = new SqlDataAdapter("select * from department", MyCon);
  7.         DataSet MyDs = new DataSet();
  8.         MyDa.Fill(MyDs);
  9.         DataTable MyDt = new DataTable();
  10.         GridView1.DataSource = MyDs.Tables[0];
  11.         GridView1.DataBind();
  12.  
  13.  
  14.     }
  15.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  16.     {
  17.         if (e.Row.RowType == DataControlRowType.DataRow)
  18.         {
  19.             string j = e.Row.Cells[0].Text;
  20.  
  21.             GridView GridView2 = (GridView)e.Row.FindControl("GridView2");
  22.             SqlDataAdapter MyDa1 = new SqlDataAdapter("select * from Emp where deptid =" +Convert.ToInt16(j), MyCon);
  23.             DataSet MyDs1 = new DataSet();
  24.             MyDa1.Fill(MyDs1);
  25.             DataTable MyDt1 = new DataTable();
  26.             GridView2.DataSource = MyDs1.Tables[0];
  27.             GridView2.DataBind();
  28.  
  29.             GridView GridView3 = (GridView)e.Row.FindControl("GridView3");
  30.             SqlDataAdapter MyDa2 = new SqlDataAdapter("select sum(salary) as SumSalary from Emp where deptid =" + Convert.ToInt16(j), MyCon);
  31.             DataSet MyDs2 = new DataSet();
  32.             MyDa2.Fill(MyDs2);
  33.             DataTable MyDt2 = new DataTable();
  34.             GridView3.DataSource = MyDs2.Tables[0];
  35.             GridView3.DataBind();
  36.         }
  37.     }
I do not know where to place the sum of the employee's salary


Thanks in advance



Regards

cmrhema
Dec 16 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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