2012-04-09 17 views
0

私は2つのグリッドを持っています.1つは自動生成フィールドで、もう1つはグリッドではありません。どのように私は2つのグリッドをエクスポートすることができますか(実際には1つにまとめられていますか?)次のコードは、メソッドとグリッドの初期のイメージを使用して2つのグリッドを結合する方法を示しています。ネストされたグリッドビューのデータを単語にエクスポートするにはどうすればよいですか?

私のコード:

protected void BindHistoryReport() 
{ 
    ArrayList listofavgrating = new ArrayList(); 
    ArrayList listofdouble = new ArrayList(); 
    ArrayList listofquestion = dbmanager.GetAllQuestion(); 
    ArrayList listofstaff = dbmanager.GetAllStaffDetails(); 
    DataTable dt = new DataTable(); 
    DataRow dr = null; 
    int count = 0; 

    foreach (staffinfo stf in listofstaff) 
    { 
     ArrayList listofhistorydates = dbmanager.GetTotalHistoryDates(stf.Uid); 
     if (count==0) 
     { 
      dt.Columns.Add(new DataColumn("UserID", typeof(string))); 
      dt.Columns.Add(new DataColumn("Name", typeof(string))); 
      dt.Columns.Add(new DataColumn("Section", typeof(string))); 
      dt.Columns.Add(new DataColumn("Function", typeof(string))); 
      count++; 
     } 

     if (listofhistorydates.Count != 0 && listofquestion.Count != 0 && count > 0) 
     { 
      dr = dt.NewRow(); 
      dr["UserID"] = stf.Uid; 
      dr["Name"] = stf.Name; 
      dr["Section"] = stf.Section; 
      dr["Function"] = stf.Function; 
      dt.Rows.Add(dr); 
     } 
    } 
    if (count > 0) 
    { 
     ViewState["HistoryGrid"] = dt; 
     Session["ListofQuestion"] = listofquestion; 
     ViewAllHistory.DataSource = dt; 
     ViewAllHistory.DataBind(); 
     BindInsideGradeGrid(); 
     LegendMessage(); 
     MultiView1.ActiveViewIndex = 0; 
    } 
    else 
    { 
     Response.Redirect("default.aspx"); 
    } 
} 

protected void BindInsideGradeGrid() 
{ 
    ArrayList listofquestion = (ArrayList)Session["ListofQuestion"]; 
    DataTable maintable = (DataTable)ViewState["HistoryGrid"]; 
    int indexgrid = 0; 
    foreach (DataRow row in maintable.Rows) 
    { 
     DataTable dt2 = new DataTable(); 
     DataRow dr2 = null; 
     string userid = row["UserID"].ToString(); 
     ArrayList listofhistorydates = dbmanager.GetTotalHistoryDates(userid); 

     for (int i = 0; i < listofhistorydates.Count; i++) 
     { 
      DateTime toshortdate=((DateTime)listofhistorydates[i]); 
      dt2.Columns.Add(new DataColumn(toshortdate.ToShortDateString(), typeof(double))); 
     } 

     int index = 0; 
     double result = 0.0; 
     dr2 = dt2.NewRow(); 
     foreach (DateTime date in listofhistorydates) 
     { 
      foreach (Question qn in listofquestion) 
      { 
       result += dbmanager.GetAvgRating(userid, date, qn.QuestionID); 
      } 
      result = Math.Round((result/listofquestion.Count), 1); 
      DateTime toshortdate = ((DateTime)listofhistorydates[index]); 
      dr2[toshortdate.ToShortDateString()] = result; 
      index++; 
      result = 0.0; 
     } 
     dt2.Rows.Add(dr2); 
     GridView gv = (GridView)ViewAllHistory.Rows[indexgrid].FindControl("GridView1"); 
     //gv.ControlStyle.Width = 500; 
     gv.DataSource = dt2; 
     gv.DataBind(); 
     indexgrid++; 
    } 
} 

デザイン:

<asp:GridView ID="ViewAllHistory" runat="server" BorderWidth="1px" 
          CellPadding="1" CellSpacing="1" BackColor="Black" 
          AutoGenerateColumns="False" PageSize="1"> 
          <RowStyle BackColor="White"/> 
          <Columns> 
           <asp:TemplateField> 
            <ItemTemplate> 
             <asp:LinkButton ID="ViewBtn" runat="server" onclick="ViewBtn_Click">View</asp:LinkButton> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:BoundField DataField="UserID" HeaderText="User ID" /> 
           <asp:BoundField DataField="Name" HeaderText="Name" > 
           </asp:BoundField> 
           <asp:BoundField DataField="Section" HeaderText="Section" /> 
           <asp:BoundField DataField="Function" HeaderText="Function" /> 
           <asp:TemplateField HeaderText="Grade"> 
            <ItemTemplate> 
             <asp:Panel ID="ScrollPanel" style="width:760px" runat="server" ScrollBars="Horizontal"> 
              <asp:GridView ID="GridView1" style="position:static" runat="server" BorderWidth="1px" BackColor="Black"> 
              <FooterStyle BackColor="#CCCCCC" /> 
              <RowStyle BackColor="White" Wrap="true"/> 
              <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
              <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
              </asp:GridView> 
             </asp:Panel> 
            </ItemTemplate> 
           </asp:TemplateField> 
          </Columns> 
          <FooterStyle BackColor="#CCCCCC" /> 
          <PagerSettings Position="TopAndBottom" /> 
          <PagerStyle HorizontalAlign="Center" BackColor="White" /> 
          <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
          <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
         </asp:GridView> 

画像:enter image description here

答えて

1

このリンクはあなたのために可能同様の問題を示しています。 How To exporting to Excel parent and nested GridView data?

グリッドの行を繰り返し、子グリッドが存在するかどうかを確認する必要があります。

+0

ここでは、私はgridviewの最初の列を削除することができますか?私はgridView.Columnsを試しました[0] .Visible = false;しかし、仕事はありません。 –

+0

HTMLでvisible = falseに設定できます。 – Mez

+0

私は理解していない、あなたは私にどこを見せてもらえますか? e.Row.RowType = DataControlRowType.DataRow OrElse _ e.Row場合 –

関連する問題