现在,我尝试使用gridview控件显示三列“高风险”、“进一步调查”和“低风险”,并在页脚处显示三列之和。实际上,每个单元格数据都已成功地从数据库中检索出来(三列之和是正确的),但奇怪的是,所有单元格都是空的,没有显示任何数字(我使用(null)表示空单元格)。您可以看到网格视图的结果,如下所示:
更多更高风险
(零)
---------------|------------|
自愿性、隐性性、无偿性、隐性性(零)、自愿性、自愿性(零)、自愿性、无偿性(零)、自愿性(零)、自愿性(零)、
---------------|------------|
(零)
---------------|------------|
和,20,000,
aspx.code:
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" style="margin-left: 354px" AllowPaging="True" AutoGenerateColumns="False" Height="269px" Width="440px" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField> </asp:BoundField>
<asp:BoundField HeaderText="High Risk" SortExpression="numberOfhigh">
<ItemStyle BackColor="Red" />
</asp:BoundField>
<asp:BoundField HeaderText="Further Risk" SortExpression="numberOffurther">
<ItemStyle BackColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="Low Risk" SortExpression="numberOflow">
<ItemStyle BackColor="#33CC33" />
</asp:BoundField>
</Columns>
</asp:GridView>代码隐藏:
if (DropDownList2.SelectedIndex == 1 && DropDownList1.SelectedIndex ==1 )
{
GridView1.Visible = true;
sqlConn.Open();
//string cmdText = "SELECT [numberOfhigh],[numberOffurther],[numberOflow] FROM [rampDB].[dbo].[Assessment] WHERE [AssessmentID] ='" + Session["assid"] + "'";
string cmdText = "SELECT [numberOfhigh],[numberOffurther],[numberOflow] FROM [rampDB].[dbo].[Assessment]";
SqlDataAdapter sda = new SqlDataAdapter(cmdText, sqlConn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}以及gridview rowdatabind事件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//sum result at the footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
sum1 += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "numberOfhigh"));
sum2 += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "numberOffurther"));
sum3 += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "numberOflow"));
}
// Display totals in the gridview footer
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Sum risk";
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[1].Font.Bold = true;
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Font.Bold = true;
e.Row.Cells[1].Text = sum1.ToString();
e.Row.Cells[2].Text = sum2.ToString();
e.Row.Cells[3].Text = sum3.ToString();
}
}发布于 2014-07-20 02:50:47
BoundField元素的GridView需要一个属性"DataField“来知道要显示什么数据。
<asp:BoundField DataField="DataTableFieldName" HeaderText="Any Text" />https://stackoverflow.com/questions/24846688
复制相似问题