首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有值的Asp.net网格查看单元

没有值的Asp.net网格查看单元
EN

Stack Overflow用户
提问于 2014-07-20 02:09:07
回答 1查看 1.1K关注 0票数 0

现在,我尝试使用gridview控件显示三列“高风险”、“进一步调查”和“低风险”,并在页脚处显示三列之和。实际上,每个单元格数据都已成功地从数据库中检索出来(三列之和是正确的),但奇怪的是,所有单元格都是空的,没有显示任何数字(我使用(null)表示空单元格)。您可以看到网格视图的结果,如下所示:

更多更高风险

(零)

---------------|------------|

自愿性、隐性性、无偿性、隐性性(零)、自愿性、自愿性(零)、自愿性、无偿性(零)、自愿性(零)、自愿性(零)、

---------------|------------|

(零)

---------------|------------|

和,20,000,

aspx.code:

代码语言:javascript
复制
<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>

代码隐藏:

代码语言:javascript
复制
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事件:

代码语言:javascript
复制
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();
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-20 02:50:47

BoundField元素的GridView需要一个属性"DataField“来知道要显示什么数据。

代码语言:javascript
复制
<asp:BoundField DataField="DataTableFieldName" HeaderText="Any Text" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24846688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档