Reputation:
I have two grid, one with auto-generated fields and the other not. how can i export two of the grid (which is actually together to one?) The following code illustrate how i bind two grid together using methods and a early image of the grid.
my 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>
image:
Upvotes: 0
Views: 520
Reputation: 4726
This link shows a possible similar problem to yours. How To exporting to Excel parent and nested GridView data?
You would need to iterate the rows of the grid, and see if any child grids exist.
Upvotes: 1