C# 如何基于gridview中的列值创建组(节)?
在我的网格中,有两列数据,两行数据相同。然后,另外两行将使用不同的数据进行相同的处理。我想用颜色将它们分成不同的部分 在下面的示例(图)中C# 如何基于gridview中的列值创建组(节)?,c#,asp.net,vb.net,C#,Asp.net,Vb.net,在我的网格中,有两列数据,两行数据相同。然后,另外两行将使用不同的数据进行相同的处理。我想用颜色将它们分成不同的部分 在下面的示例(图)中 我们如何才能做到这一点?您可以检查每行两列的值,并执行此处所述的操作: 待处理事件: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow)
我们如何才能做到这一点?您可以检查每行两列的值,并执行此处所述的操作: 待处理事件:
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子页\u PreRender(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.PreRender
Dim dt作为新的Data.DataTable
Add(“行号”,GetType(Int32))
添加(“年龄”,GetType(字符串))
dt.Columns.Add(“年销售额”,GetType(字符串))
添加(“分类”,GetType(字符串))
增加(1,“高”、“高”、“核心”)
增加(5,“高”、“低”、“核心”)
增加(9,“高”、“中”、“核心”)
GridView1.DataSource=dt
GridView1.DataBind()
端接头
私有子GridView1_RowDataBound(ByVal sender作为对象,ByVal e作为System.Web.UI.WebControl.GridViewRowEventArgs)处理GridView1.RowDataBound
如果e.Row.RowType DataControlRowType.DataRow,则
出口接头
如果结束
Dim dr=DirectCast(e.Row.DataItem,Data.DataRowView).Row
选择案例DirectCast(dr(“年度销售额”),字符串)
案例“高”
e、 Row.BackColor=Drawing.Color.Gray
案例“低”
e、 Row.BackColor=Drawing.Color.White
案例“中等”
e、 Row.BackColor=Drawing.Color.Gray
结束选择
端接头
好的——修改Leniel Macaferi的答案:(我混合了一点w/vb,因为我不是一个c类人——对不起——尽管我希望这一点还是很清楚)
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//以斜体显示公司名称。
Session(“New_Pair_1”)=e.Row.Cells[1]。Text
Session(“New_Pair_2”)=e.Row.Cells[2]。Text
如果会话(“新对1”)会话(“旧对1”)或_
会话(“新配对”)会话(“旧配对”)然后
//与上一张唱片的配对不匹配
//第一步,改变背景色
如果e.Row.BackColor=Color.FromName(“灰色”),则
e、 Row.BackColor=Color.FromName(“白色”)
其他的
e、 RowlBackColor=Color.FromName(“灰色”)
如果结束
//步骤2,重置“旧”会话变量
会话(“旧对1”)=会话(“新对1”)
会话(“旧对”会话2)=会话(“新对”会话2)
如果结束
}
}
我的数据列是动态的。非静态
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");
}
}
}
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
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
}
}