2014-09-11 59 views
1

我有网格,在该网格中有四列具有“数据格式”属性。两列根据“数据格式”给出结果。但是在“数据格式”中没有提到两栏。这里我们在四栏中的“数据格式”属性中提到小数点后两位值格式。Gridview DataFormatingString不适用于某些列

下面是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; 

       } 
      } 
     } 
     }  

为 “胡志明市” 和 “MAXH” ,它不显示小数点后两位值,但“Min_OL”和“Max_OL”显示小数点后两位值。 我试过DataFormatString =“{0:0.00}”,但那个也不起作用。

+0

你检查你在胡志明市(MAXH)和Min_OL(Max_OL)的情况下提供输入相同。 ?(每种情况下都是十进制) – 2014-09-11 03:15:14

+0

@AshokRathod在这两种情况下的数据类型都是双重的 – vim 2014-09-11 03:20:07

回答

2

请检查您的数据的来源 - 如果它是数据库,请确保SQL类型包含小数(强制使用乘以1.0)。如果其他来源,检查相同。

您可以检查数据,通过使用下面的模板场降临的类型:

<asp:TemplateField HeaderText="minH DataType"> 
    <ItemTemplate><%#Eval("minH").GetType()%></ItemTemplate> 
</asp:TemplateField> 
+1

您的代码<%#Eval(“minH”)。GetType()%> 给出错误 – vim 2014-09-11 04:11:56

+1

它说“代码块在此上下文中不受支持” – vim 2014-09-11 04:15:26

+0

不,它不在itemtemplet中工作。错误在那里。 – vim 2014-09-11 05:50:33

0

请尝试简单{0:F}而不是{0:F2}。并检查所有四个值中的数据格式是否相同。

+0

兄我也试过{0:F}但结果相同,数据类型都是Double,所有四列 – vim 2014-09-11 03:31:45

+0

在这种情况下,请验证您的列名是否正确并且在执行过程中没有发生任何异常。验证在数据源中存在正确的列并且它们具有相同的名称 – 2014-09-11 03:48:09

+0

没有错误,我得到那个网格,但唯一的事情是两列Min_OL和Max_OL显示了小数点后两位的值,但另外两个minH和maxH显示的是十进制后的两个以上的地方。我只是编辑了问题并将该代码放在function.please check – vim 2014-09-11 03:55:42

相关问题