Brian
asked on
GridView Footer Values
Hello,
I have the following GridView listed below along with the following code below and I'm trying to retrieve values from the DataTable to the Footer Template of my GridView. The values do retrieve to just label controls only.
<asp:GridView ID="PrintReport" runat="server" AutoGenerateColumns="False "
CellPadding="4" EnableModelValidation="Tru e" ForeColor="#333333"
GridLines="None" Width="500px" EnableViewState="False" HorizontalAlign="Left" ShowFooter="true">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="asgmt_title" HeaderText="Assignment">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="asgmt_pntspossi ble" HeaderText="Pts. Possible">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="asgmt_pntsearne d" HeaderText="Pts. Earned">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
<PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
<RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#F0F0F0" Font-Bold="True" ForeColor="Navy" Height="20px" HorizontalAlign="Left" />
</asp:GridView>
<br />
<br />
<asp:Label ID="lblPtsPossible" Text="Total Pts. Possible: " runat="server"></asp:Label > &nb sp; & nbsp;<asp: Label ID="lblPtsEarned" Text="Total Pts Earned: " runat="server"></asp:Label >
protected void Page_Load(object sender, EventArgs e)
{
string student_id;
student_id = Request.QueryString["stude nt_id"];
string course_id;
course_id = Request.QueryString["cours e_id"];
SqlConnection conn = new SqlConnection(Configuratio nManager.C onnectionS trings["BB Grades"].C onnectionS tring);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "BB-Grades_PrintAssignment s";
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Connection = conn;
cmd.Parameters.AddWithValu e("@studen t_id", student_id);
cmd.Parameters.AddWithValu e("@course _id", course_id);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
adp.Fill(dt);
try
{
conn.Open();
PrintReport.DataSource = dt;
PrintReport.DataBind();
foreach (DataRow row in dt.Rows)
{
lblPtsPossible.Text = row["fg_pntspossible"].ToS tring();
lblPtsEarned.Text = row["fg_pntsearned"].ToStr ing();
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
I have the following GridView listed below along with the following code below and I'm trying to retrieve values from the DataTable to the Footer Template of my GridView. The values do retrieve to just label controls only.
<asp:GridView ID="PrintReport" runat="server" AutoGenerateColumns="False
CellPadding="4" EnableModelValidation="Tru
GridLines="None" Width="500px" EnableViewState="False" HorizontalAlign="Left" ShowFooter="true">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="asgmt_title" HeaderText="Assignment">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="asgmt_pntspossi
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="asgmt_pntsearne
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
<PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
<RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#F0F0F0" Font-Bold="True" ForeColor="Navy" Height="20px" HorizontalAlign="Left" />
</asp:GridView>
<br />
<br />
<asp:Label ID="lblPtsPossible" Text="Total Pts. Possible: " runat="server"></asp:Label
protected void Page_Load(object sender, EventArgs e)
{
string student_id;
student_id = Request.QueryString["stude
string course_id;
course_id = Request.QueryString["cours
SqlConnection conn = new SqlConnection(Configuratio
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "BB-Grades_PrintAssignment
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = conn;
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
adp.Fill(dt);
try
{
conn.Open();
PrintReport.DataSource = dt;
PrintReport.DataBind();
foreach (DataRow row in dt.Rows)
{
lblPtsPossible.Text = row["fg_pntspossible"].ToS
lblPtsEarned.Text = row["fg_pntsearned"].ToStr
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
Hi,
You can use the RowDataBound event of your GridView to populate your footer. The RowDataBound event occurs when a data row is bound to data in a GridView control.
Inside the event, you can then inspect e.Row.RowType to determine the row type (e.g. Header, Footer, DataRow) and once it is a footer you can then access the cells e.Row.Cells in your footer or you could access your template controls (e.g. Label) by using FindControl function.
You can use the RowDataBound event of your GridView to populate your footer. The RowDataBound event occurs when a data row is bound to data in a GridView control.
Inside the event, you can then inspect e.Row.RowType to determine the row type (e.g. Header, Footer, DataRow) and once it is a footer you can then access the cells e.Row.Cells in your footer or you could access your template controls (e.g. Label) by using FindControl function.
ASKER
Hi Alfred1,
Is there any way that you could show me how to access the template controls (Label) controls by using the FindControl function rather than implementing the RowDataBound Event?? The reason why is because I would hate to implement the RowDataBound Event for only two values.
Thanks in advance!!
Is there any way that you could show me how to access the template controls (Label) controls by using the FindControl function rather than implementing the RowDataBound Event?? The reason why is because I would hate to implement the RowDataBound Event for only two values.
Thanks in advance!!
Hi,
Try this, example FindControlRecursive(Print Report,lbl PtsPossibl e)
Try this, example FindControlRecursive(Print
public Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
ASKER
How do I tie this into what I currently have?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://aspalliance.com/782