2012-04-09 2 views
0

Ich habe zwei Gitter, eines mit automatisch generierten Feldern und das andere nicht. Wie kann ich zwei der Gitter exportieren (was eigentlich zusammen ist?) Der folgende Code zeigt, wie ich zwei Gitter mit Methoden und einem frühen Bild des Gitters binde.Wie kann ich verschachtelte Gridview-Daten in Word exportieren?

mein Code:

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++; 
    } 
} 

Design:

<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> 

Bild: enter image description here

Antwort

1

Dieser Link zeigt eine mögliche ähnliches Problem bei Ihnen. How To exporting to Excel parent and nested GridView data?

Sie müssten die Zeilen des Gitters durchlaufen und prüfen, ob untergeordnete Gitter vorhanden sind.

+0

hey einen kleinen Gefallen hier, weißt du, ob ich die erste Spalte der Gridview entfernen kann? ich habe versucht, gridView.Columns [0] .Visible = false; aber nicht arbeiten. –

+0

Sie können die Spalte im HTML-Code auf visible = false setzen. – Mez

+0

ich verstehe nicht, du meinst mir wo zeigen? –

Verwandte Themen