asp.net如何在gridview上添加条件

时间:2012-07-30 12:41:07

标签: asp.net gridview conditional-statements

所以我已经将网格连接到数据库,其中我的数据库只包含1,2和3的值。我想拥有它,以便每个具有1的框是绿色,2是黄色,3是红色

我的问题是我应该在哪里放置调节代码以及我应该使用哪种语言?

以下信息只是我的gridview以及我将其链接到的数据。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1"
    >
    <Columns>
        <asp:BoundField DataField="Line" HeaderText="Line"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" ItemStyle-BackColor="#FF6699" />
        <asp:BoundField DataField="Jan" HeaderText="Jan"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Feb" HeaderText="Feb"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Mar" HeaderText="Mar"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Apr" HeaderText="Apr"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="May" HeaderText="May"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Jun" HeaderText="Jun"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Jul" HeaderText="Jul"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Aug" HeaderText="Aug"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Sep" HeaderText="Sep"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Oct" HeaderText="Oct"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Nov" HeaderText="Nov"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Dec" HeaderText="Dec"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />

    </Columns>
</asp:GridView>



<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:dashboardConnectionString %>" 
SelectCommand="SELECT 
        line AS Line, 
        [2012-01-31] AS 'Jan', 
        [2012-02-29] AS 'Feb', 
        [2012-03-31] AS 'Mar', 
        [2012-04-30] AS 'Apr', 
        [2012-05-31] AS 'May', 
        [2012-06-30] AS 'Jun', 
        [2012-07-31] AS 'Jul', 
        [2012-08-31] AS 'Aug', 
        [2012-09-30] AS 'Sep', 
        [2012-10-31] AS 'Oct', 
        [2012-11-30] AS 'Nov', 
        [2012-12-31] AS 'Dec'
    FROM
    (Select line, report_month, state FROM [Monthly_State]) AS sourcetable
    PIVOT
    (
    MAX(state) 
    FOR report_month IN ([2012-01-31], [2012-02-29], [2012-03-31], [2012-04-30], [2012-05-31], [2012-06-30], [2012-07-31], [2012-08-31], [2012-09-30], [2012-10-31], [2012-11-30], [2012-12-31])
    ) as pivottable; ">

    </asp:SqlDataSource>

3 个答案:

答案 0 :(得分:1)

由于您无法在CssClass属性中使用数据绑定表达式,因此必须使用后面的代码。 首先,您必须将列转换为模板。 而不是:

<asp:BoundField DataField="Jan" HeaderText="Jan"  ItemStyle-Width="75" ItemStyle-HorizontalAlign="Right" />

写:

<asp:TemplateField HeaderText="Jan">
   <ItemTemplate>
      <asp:Label ID="lblJan" runat="server" Text="<%# Eval("Jan") %>" />
   </ItemTemplate>
</asp:TemplateField>

然后你可以使用RowDataBound来查找你的e.Row.FindControl("lblJan")控件,并根据它的值为它设置css类。

答案 1 :(得分:0)

在网格视图的行数据绑定事件中,您必须放置条件。

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   if(e.Row.RowType == DataControlRowType.DataRow)
   {
    // example 
    // You have to give the correct index ( starts from 0 ). Cell[0] will be the first column in your grid
       if(  e.Row.Cells[0].Text = "1" )
       {
           e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
       }
       if(  e.Row.Cells[1].Text = "2" )
       {
           e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
       }
       if(  e.Row.Cells[2].Text = "3" )
       {
           e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
       }
   // In the same way do for other columns

   }

 }

答案 2 :(得分:0)

我会通过使用模板字段来实现它,并在后面的代码中声明一个公共方法,如下所示

<asp:TemplateField> <ItemTemplate> <asp:Label ID="cddasf" runat="server" CssClass='<%# GetClass(Eval("colum name")) %>'></asp:Label> </ItemTemplate> </asp:TemplateField>

在背后的代码中

public  string GetClass(object g)
{

//处理该值并返回您想要的css类名

}