如何根据gridview中的列值创建组(部分)?
问题描述:
在我的网格中,我有两列数据相同的几行。然后再次其他行的几个将与不同的数据相同。我想让他们成为颜色替代部分如何根据gridview中的列值创建组(部分)?
在下面的例子(图片)。
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.
我们怎样才能做到这一点?
答
OK - 只要修改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
}
}
答
你可以检查两列的值的每一行做什么,在这里描述:
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");
}
}
}
答
最直接的方法是绑定将gridview模板中的背景值转换为后面代码中的方法,将您关心的两个字段作为参数,然后切换这些值以返回适当的颜色字符串。
这不是最优雅的方法,我认为有可能一些自然的分组机制内置到gridview我可能不知道,但这是很容易实现和使用一个案件。看看更自然的事情,如果你是一个坚持者或者要在很多地方实现这一点。
答
这是一个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
我的数据列是动态的。不是静态的 – James123 2010-07-26 21:27:26