还有一个问题,帮忙解决!
把数据库中的数据显示在gridview控件中。能不能根据表中的某一列(表示颜色如红,黄,蓝)使另一列的数据(如123,234,345)变成该指定颜色的数据
这列的数据123是红色的,234是黄色的,345是蓝色的
谢谢! --------------------编程问答-------------------- 在RowDataBound中判断 --------------------编程问答-------------------- 能不能详细些啊?
给些代码更好 --------------------编程问答-------------------- 表达式绑定。 --------------------编程问答-------------------- 你在RowDataBound事件中写,根据你的不同的数据做判断,就可以了..
如果使用了数据绑定,可以触发datagridview的RowDataBound事件
protected void GridViewTransportPapers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(....)
{
e.Row.BackColor = Color.Red;
}
else if(...)
{
e.Row.BackColor = Color.Blue;
}
else e.Row.BackColor = Color.yellow;
}
} --------------------编程问答-------------------- RowDataBound 这个是对的..在数据绑定后触发..
循环遍历控件的每行,然后做相关的处理 --------------------编程问答--------------------
asp:TemplateField HeaderText="age" SortExpression="age">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>' ForeColor='<%# GetAgeColor((int)Eval("Age")) %>'></asp:Label>
</ItemTemplate>
protected System.Drawing.Color GetAgeColor(int age)
{
if (age < 21)
return System.Drawing.Color.Red;
else if (age < 74)
return System.Drawing.Color.Blue;
else
return System.Drawing.Color.Green;
}
这是根据数据源来计算。所以,本列显示人名,但是根据此人的年龄来绑定不同ForeColor。 --------------------编程问答--------------------
<asp:TemplateField HeaderText="姓名">--------------------编程问答-------------------- Up --------------------编程问答-------------------- 学习6楼 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 1楼和6、7楼分别是两种方式,都可以 --------------------编程问答--------------------
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>' ForeColor='<%# GetAgeColor((int)Eval("Age")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
具体做法是这样的:
--------------------编程问答-------------------- up --------------------编程问答-------------------- 上面的兄弟给的代码,很细了,不多说了。
protected void GridView1_RowDataBount(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string key = DataBinder.Eval(e.Row.DataItem, "颜色").ToString();
if(key=="红")
{
e.Rows.cell[3].text="<font color=red>"+e.Rows.cell[3].text+"</font>";//假定你想改变颜色的是GridView的第四列(从零开始编号的)
}
}
}
补充:.NET技术 , ASP.NET