如何在GridView中根据列值创建分组(section)?

4

在我的网格中,有两列相同的数据,对于几行而言是一样的。然后另外几行也是相同的,但数据不同。我想将它们变成交替的部分,并加上颜色。

如下图所示。

Rows no 1 to 4 has 'High', 'High'. I want make them gray bgcolor  for those rows.
Rows no 5 to 8 has 'High','Low'. I want make them white bgcolor for those rows
Rows no 9 to 12 has 'High','Medium'. I want make them again gray bg color for 
those rows.

alt text

我们该如何做到呢?

4个回答

0
你可以检查每一行的两个列的值,并执行这里描述的操作: 更改GridView行的背景颜色 要处理的事件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       // Display the company name in italics.
       if(e.Row.Cells[1].Text.Equals("High") &&
          e.Row.Cells[2].Text.Equals("High"))
       {
           e.Row.BackColor = Color.FromName("Gray");
       }
       else if(e.Row.Cells[1].Text.Equals("High") &&
               e.Row.Cells[2].Text.Equals("Low"))
       {
           e.Row.BackColor = Color.FromName("White");
       }
       else if(e.Row.Cells[1].Text.Equals("High") &&
               e.Row.Cells[2].Text.Equals("Medium"))
       {
          e.Row.BackColor = Color.FromName("Gray"); 
       }
    }
}

我的数据列是动态的,而不是静态的。 - James123

0

最直接的方法是将GridView模板中的背景值绑定到代码后台中的一个方法,该方法以您关心的两个字段作为参数,并根据这些值切换返回适当的颜色字符串。

我认为这不是最优雅的方法,可能GridView内置了一些自然分组机制,我可能不知道,但对于单个情况,这种方法简单易用。如果您是一个苛求完美的人或者要在许多地方实现此功能,请查看更自然的东西。


0
这是一个VB的例子。它创建了一个与您拥有的可比较的模式,并在“RowDataBound”事件期间完成工作。
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    Dim dt As New Data.DataTable
    dt.Columns.Add("Row No", GetType(Int32))
    dt.Columns.Add("Age", GetType(String))
    dt.Columns.Add("Annual Sales", GetType(String))
    dt.Columns.Add("Assortment", GetType(String))

    dt.Rows.Add(1, "High", "High", "CORE")
    dt.Rows.Add(5, "High", "Low", "CORE")
    dt.Rows.Add(9, "High", "Medium", "CORE")

    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType <> DataControlRowType.DataRow Then
        Exit Sub
    End If

    Dim dr = DirectCast(e.Row.DataItem, Data.DataRowView).Row

    Select Case DirectCast(dr("Annual Sales"), String)
        Case "High"
            e.Row.BackColor = Drawing.Color.Gray
        Case "Low"
            e.Row.BackColor = Drawing.Color.White
        Case "Medium"
            e.Row.BackColor = Drawing.Color.Gray
    End Select
End Sub

0

好的 - 只需修改Leniel Macaferi的答案:(我有点混淆了VB,因为我不是C#的人 - 抱歉 - 但我希望重点仍然很清楚)

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       // Display the company name in italics.
       Session("New_Pair_1") = e.Row.Cells[1].Text
       Session("New_Pair_2") = e.Row.Cells[2].Text

       If Session("New_Pair_1") <> Session("OLD_Pair_1") OR _
          Session("New_Pair_2") <> Session("OLD_Pair_2") Then
             //no pair match with last record's pair
             //step 1, change the backcolor
               If e.Row.BackColor = Color.FromName("Gray") Then
                  e.Row.BackColor = Color.FromName("White")
               Else
                  e.RowlBackColor = Color.FromName("Gray")
               End If
             //Step 2, reset the "OLD" session vars
               Session("OLD_Pair_1") = Session("New_Pair_1")
               Session("OLD_Pair_2") = Session("New_Pair_2")
       End If
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接