【问题标题】:how make groups(sections) based on column values in gridview?如何根据gridview中的列值制作组(部分)?
【发布时间】:2010-07-26 20:24:21
【问题描述】:

在我的网格中,我在两列中有几行相同的数据。然后再一次,其他几行将与不同的数据相同。我想让它们变成带有颜色的替代部分

在下面的例子中(图片)。

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.

我们该怎么做?

【问题讨论】:

    标签: c# asp.net vb.net


    【解决方案1】:

    好的——只需修改 Leniel Macaferi 的答案:(我正在混合一点 w/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
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      这不是我认为的最优雅的方法,gridview 中可能内置了一些我可能不知道的自然分组机制,但这对于单个案例来说很容易实现和使用。如果您是一个坚持者或打算在许多地方实施这一点,请看一些更自然的东西。

      【讨论】:

        【解决方案3】:

        这是一个 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
        

        【讨论】:

          【解决方案4】:

          您可以检查每一行的两列的值并执行此处描述的操作:

          Change background color of GridView's Rows

          要处理的事件:

          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"); 
                 }
              }
          }
          

          【讨论】:

          • 我的数据列是动态的。不是静态的
          猜你喜欢
          • 1970-01-01
          • 2022-01-19
          • 2020-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多