asp.net C#Gridview包含数据,但只返回cell value =“”

时间:2018-04-25 05:40:06

标签: c# asp.net gridview datatable

我遇到了以下问题。 我有一个ASP.NET Gridview,它包含12列(每月一列),只有一行显示基于sql查询匹配这些列的'double'类型的值。

GridView with data based on the their date

在这里,我为您提供了执行查询以接收匹配数据的代码段。这可以正常工作,如上图所示。

protected void LoadZusammenfassung()
{
    List<string> tempList = new List<string>();
    DataTable dt_zusammenfassung = new DataTable();

    int countEmpty = 0;

    //Erstellen der DataTable für die Monatszusammenfassung (12 Spalten)
    for (int i = 1; i <= 12; i++)
    {
        dt_zusammenfassung.Columns.Add(i.ToString(), typeof(Double));
    }

    using(SqlConnection con = new SqlConnection(MDE_ConnectionString))
    {
        con.Open();

        //Durchläuft die Monate 1-12 um die summierten Überstunden den einzelnen Spalten zuordnen zu können
        for (int i = 1; i <= 12; i++)
        {
            _query_zusammenfassung = "SELECT SUM(Anz_ueberstunden) FROM ERP WHERE Month(Ueberstundendatum) = '" + i.ToString() + "'";

            if (DDL_user.SelectedValue != "Alle Benutzer")
            {
                _query_zusammenfassung = _query_zusammenfassung + " AND UserName = '" + DDL_user.SelectedValue + "'";
            }
            if (DDL_year.SelectedValue != "Gesamter Zeitraum")
            {
                _query_zusammenfassung = _query_zusammenfassung + " AND YEAR(Ueberstundendatum) = '" + DDL_year.SelectedValue + "'";
            }

            using (SqlCommand cmd = new SqlCommand(_query_zusammenfassung, con))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (reader.HasRows)
                        {
                            if (reader[0] == null || reader[0] == DBNull.Value)
                            {
                                //tempList.Add("0.00");
                            }
                            else
                            {
                                tempList.Add(reader[0].ToString());
                            }

                        }
                    }
                }
            }
        }
        dt_zusammenfassung.Rows.Add(tempList.ToArray());

        foreach(string item in tempList)
        {
            if(item == "" || item == DBNull.Value.ToString() || item == null)
            {
                countEmpty++;
            }
        }

        if (countEmpty < 12)
        {
            gridView_zusammenfassung.DataSource = dt_zusammenfassung;
            gridView_zusammenfassung.DataBind();
        }

        UpdatePanelZusammenfassung.Update();
    }
}

在此之后,我想分析gridview中的数据,例如“任何大于20的值”。这发生在gridview的DataBound事件中。

protected void gridView_zusammenfassung_DataBound(object sender, EventArgs e)
{
    double CellValue;

    foreach (GridViewRow row in gridView_zusammenfassung.Rows)
    {
        for (int i = 0; i < gridView_zusammenfassung.Columns.Count; i++)
        {
            string Value = row.Cells[i].Text;
            //string Value = Convert.ToString(gridView_zusammenfassung.Rows[row.RowIndex].Cells[0].Text);

            if (double.TryParse(Value, out CellValue))
            {
                if (CellValue > 20)
                {
                    row.Cells[i].ForeColor = Color.Red;
                    row.Cells[i].Font.Bold = true;
                }
            }
            //else if (string.IsNullOrEmpty(row.Cells[i].Text))
            //{
            //    row.Cells[i].Text = "-";
            //}
        }
    }
}

还有问题。 “row.Cells [i] .Text”总是返回并清空字符串,如“”。我不知道这可能是什么。

- 编辑 -

我不知道它是否有帮助,但这里是Updatepanel中gridview的html部分

<%-- GridView Zusammenfassung --%>
        <td style="padding-left:20px; padding-top:36px;">
            <asp:UpdatePanel runat="server" ID="UpdatePanelZusammenfassung" UpdateMode="Conditional">
                <ContentTemplate>
                        <div runat="server" class="gridview" style="overflow-y:hidden; width:1500px; display:inline;">
                            <asp:GridView runat="server" ID="gridView_zusammenfassung" AllowPaging="false" CssClass="table table-striped table-bordered table-hover" BackColor="White" BorderColor="#009933" AutoGenerateColumns="false" HeaderStyle-BackColor="Green" OnDataBound="gridView_zusammenfassung_DataBound">

                                <Columns>
                                    <asp:TemplateField HeaderStyle-Height="57px" ItemStyle-Height="50px" HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Jan" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_jan" runat="server" Text='<%# Eval("1")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Feb" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_feb" runat="server" Text='<%# Eval("2")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Mär" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_mar" runat="server" Text='<%# Eval("3")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Apr" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_apr" runat="server" Text='<%# Eval("4")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Mai" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_mai" runat="server" Text='<%# Eval("5")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Jun" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_jun" runat="server" Text='<%# Eval("6")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Jul" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_jul" runat="server" Text='<%# Eval("7")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Aug" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_aug" runat="server" Text='<%# Eval("8")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Sep" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_sep" runat="server" Text='<%# Eval("9")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Okt" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_okt" runat="server" Text='<%# Eval("10")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Nov" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_nov" runat="server" Text='<%# Eval("11")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Dez" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl_dez" runat="server" Text='<%# Eval("12")%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>

                                <EmptyDataTemplate>
                                    Keine Daten vorhanden.
                                </EmptyDataTemplate>
                        </asp:GridView>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>
        </td>

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的答案......

我有点傻哑:)。

 <asp:TemplateField HeaderStyle-Height="57px" ItemStyle-Height="50px" HeaderStyle-CssClass="GridLines2" Visible="true" ItemStyle-CssClass="GridLines2" FooterStyle-CssClass="GridLines2" HeaderText="Jan" HeaderStyle-Wrap="false" FooterStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-Width="100px"> 
    <ItemTemplate>
        <asp:Label ID="lbl_jan" runat="server" Text='<%# Eval("Jan")%>' />
    </ItemTemplate>
 </asp:TemplateField>

在这部分中你可以看到我使用asp:标签来保存我的值并处理它们。

所以我不能在细胞中接收这些值的原因只是因为他们从未在那里。他们一直都在这些标签中。

我只需要使用以下行来查询所有这些标签

string CurrentLblValue = (gridView_zusammenfassung.Rows[row.RowIndex].FindControl("lbl_" + gridView_zusammenfassung.Columns[i].HeaderText.ToLower()) as Label).Text;