Gridview DataFormatingString对某些列不起作用

本文关键字:不起作用 DataFormatingString Gridview | 更新日期: 2023-09-27 18:21:31

我有一个网格,其中四列具有"数据格式"属性。两列根据"数据格式"给出结果。但有两列没有采用"数据格式"中提到的条件。这里我们在四列的"数据格式"属性中提到十进制两位值格式。

这里是aspx代码:

    <asp:GridView ID="GridView2" runat="server" AllowPaging="true" PageSize="5" 
       AutoGenerateColumns="false" Width="100%" OnPageIndexChanging="GridView2_PageIndexChanging"
        OnRowDataBound="GridView2_RowDataBound" CssClass="Grid">
               <RowStyle CssClass="GridRow"/>
                 <Columns>
                     <asp:BoundField HeaderText="No" DataField="id" Visible="false"/>
                     <asp:BoundField HeaderText="Scenario" DataField="Scenario"/>
                     <asp:BoundField HeaderText="Type" DataField="Type"/>
                     <asp:BoundField HeaderText="Station Name" DataField="StationName"/>
                     <asp:BoundField HeaderText="Action" DataField="Action"/>
                     <asp:BoundField HeaderText="minH" DataField="minH" 
                       SortExpression="minH" DataFormatString="{0:F2}"/>
                     <asp:BoundField HeaderText="maxH" DataField="maxH" 
                       SortExpression="maxH" DataFormatString="{0:F2}"/>
                     <asp:BoundField HeaderText="Min Level" DataField="Min_OL" 
                       SortExpression="Min_OL" DataFormatString="{0:F2}" />
                     <asp:BoundField HeaderText="Max Level" DataField="Max_OL" 
                       SortExpression="Max_OL" DataFormatString="{0:F2}" />
                </Columns>
                     <PagerStyle BackColor="White" Height="40px" Font-Bold="true" Font-
                       Size="Medium" ForeColor="Green" HorizontalAlign="Center"/>
                     <PagerSettings FirstPageText="First" LastPageText="Last" 
                       Mode="NumericFirstLast" PageButtonCount="3" />
                     <HeaderStyle BackColor="#ABDB78" ForeColor="Black" Height="35px" Font-
                       Size="13px" Font-Names="verdana"/>
                </asp:GridView>

我还在这里放了函数背后的代码:

                 protected void PumpGridBind()
                 {
                  string name = Request.QueryString[1].ToString();
                  string query = "select q1.ID  ,  q1.Scenario, q1.Type,  
                  q1.StationName ,q1.MinH, q1.MaxH ,q1.Station_Id, q1.Min_OL, q1.Max_OL,
                  q2.Daily_Abstraction as Action from (select  
                   SD.id,SD.Scenario,PR.Type,PR.StationName,max(if(PARAM = 'minH', Value, ' 
                  -999.00')) as 'minH',max(if(PARAM = 'maxH', Value, ' -999.00')) 
                  as 'maxH',psd.Station_Id,psd.Min_OL,psd.Max_OL from sgwebdb.param_reference as 
                  PR Inner join sgwebdb.scenario_data as SD ON PR.Param_Id = SD.Param_Id INNER 
                  JOIN sgwebdb.qualicision_detail as Q ON SD.SCENARIO = Q.Alternative INNER JOIN
                  sgwebdb.pump_station_detail as psd ON psd.Station_Id = PR.Station_Id where  
                  PR.Type = 'Pump' and Q.Alternative = '" + name + "' GROUP BY PR.Id) q1 JOIN 
                  (SELECT t1.Daily_Abstraction ,t1.Station_id  FROM sgwebdb.pump_station_data t1
                  INNER JOIN (SELECT Station_id, MAX(lastupdate) as lastupdate FROM 
                  sgwebdb.pump_station_data  GROUP BY Station_id ) t2 ON t1.Station_id = 
                  t2.Station_id AND t1.lastupdate = t2.lastupdate) q2 on 
                  q1.Station_Id=q2.Station_Id";
        this.GridView2.DataSource = PSI.DataAccess.Database.DatabaseManager.GetConnection
        ().GetData(query);
        GridView2.DataBind();
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        PumpGridBind();
    }

RowDataBound函数:

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
              if (e.Row.RowType == DataControlRowType.DataRow)
           {
              string iText = e.Row.Cells[5].Text;
              if (iText == "-999.00")
              {
                e.Row.Cells[5].Text = "-999.00";
              }
              else
              {
                double num = Convert.ToDouble(iText);
                string jText = e.Row.Cells[7].Text;
                double min = Convert.ToDouble(jText);
                string kText = e.Row.Cells[8].Text;
                double max = Convert.ToDouble(kText);

                if (num >= min && num < max)
                {
                    //e.Row.Cells[5].CssClass = "GridCond2";
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Purple;
                }
                else if (num >= max)
                {
                    //e.Row.Cells[5].CssClass = "GridCond1";
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    //e.Row.Cells[5].CssClass = "GridCond3";
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Black;
                }
            }
         }
       }    

对于"minH"answers"maxH",它不显示小数点后两位的值,但"Min_OL"answers"Max_OL"显示小数点前两位的数值。我尝试了DataFormatString="{0:0.00}",但该字符串也不起作用。

Gridview DataFormatingString对某些列不起作用

请检查数据源-如果是数据库,请确保SQL类型包括小数(通过乘以1.0强制执行)。如果是其他源,请检查相同的数据源。

您可以使用以下作为模板字段来检查通过的数据类型:

<asp:TemplateField HeaderText="minH DataType">
  <ItemTemplate><%#Eval("minH").GetType()%></ItemTemplate>
</asp:TemplateField> 
请尝试简单的{0:F}而不是{0:F2}。并且还检查所有四个值中的数据格式是否相同。