0

我有两个网格,一个带有自动生成的字段,另一个没有。我如何导出两个网格(实际上是一个网格?)下面的代码说明了我如何使用方法和网格的早期图像将两个网格绑定在一起。

我的代码:

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>

图片:在此处输入图像描述

4

1 回答 1

1

此链接显示了与您的可能类似的问题。如何导出到 Excel 父级和嵌套的 GridView 数据?

您需要迭代网格的行,并查看是否存在任何子网格。

于 2012-04-09T08:31:01.523 回答