在 GridView 中设置 DataRow 的 ForeColor ASP.NET(不绑定到 DataSource)
Setting ForeColor of DataRow in GridView ASP.NET (Not bounded to DataSource)
我更改了第一行单元格的颜色,但我不希望第二行(分配床位数)受到影响。我可以知道我该怎么做吗?
protected void bedStatsClass_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[1];
int number = int.Parse(cell.Text);
if (number <= 10)
{
cell.ForeColor = Color.Red;
cell.Style.Add("font-weight", "bold");
}
else if (number > 10)
{
cell.ForeColor = Color.ForestGreen;
cell.Style.Add("font-weight", "bold");
}
//So on and so forth..
}
}
图片说明
感谢您的帮助。谢谢。
您可以使用RowIndex 来检查行号。然后,您可以根据该数字为元素着色。
protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
//do stuff with the first row
}
if (e.Row.RowIndex % 2 == 0)
{
//if you want to do something to rows 1, 3, 5 etc
}
}
}
数据行有完整的解决方案,即网格视图的标签,但不在数据源的数据绑定中,我们可以在没有 OnRowDataBound 的情况下直接使用。请尽量做到这一点 solution.so 我们有一个例子
在 aspx 页面中:
<asp:GridView ID="GridViewBalance" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="card_no" GridLines="None" CssClass="table table-striped"
OnPageIndexChanging="GridViewBalance_PageIndexChanging" PageSize="50" AllowPaging="True"
CellSpacing="4">
<Columns>
<asp:BoundField DataField="card_no" HeaderText="Card No" />
<asp:BoundField DataField="acc_no" HeaderText="Account No" />
<asp:BoundField DataField="cname" HeaderText="Name" />
<asp:BoundField DataField="mobileno" HeaderText="Mobile No" />
<asp:BoundField DataField="balance" HeaderText="Balance" />
<asp:BoundField DataField="created_date" HeaderText="Creation date" DataFormatString="{0:d/M/yyyy HH:mm:ss}" />
<asp:TemplateField HeaderText="Active-Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" ForeColor="White" Text='<%# GetStatus(Eval("is_active").ToString()) %>' BackColor='<%# GetColor(Eval("is_active").ToString()) %>' ToolTip="Transaction Status"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
隐藏代码:
This is used for the color of the grid view data rows.
public System.Drawing.Color GetColor(string butColor)
{
System.Drawing.Color cr = new System.Drawing.Color();
if (butColor == "1")
{
cr = System.Drawing.Color.Green;
}
else if (butColor == "0")
{
cr = System.Drawing.Color.Red;
}
return cr;
}
This is used for the status:
protected string GetStatus(string butStatus)
{
if (butStatus == "1")
{
butStatus = "ACTIVE";
}
else if (butStatus == "0")
{
butStatus = "DEACTIVE";
}
return butStatus;
}
我更改了第一行单元格的颜色,但我不希望第二行(分配床位数)受到影响。我可以知道我该怎么做吗?
protected void bedStatsClass_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[1];
int number = int.Parse(cell.Text);
if (number <= 10)
{
cell.ForeColor = Color.Red;
cell.Style.Add("font-weight", "bold");
}
else if (number > 10)
{
cell.ForeColor = Color.ForestGreen;
cell.Style.Add("font-weight", "bold");
}
//So on and so forth..
}
}
图片说明
感谢您的帮助。谢谢。
您可以使用RowIndex 来检查行号。然后,您可以根据该数字为元素着色。
protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
//do stuff with the first row
}
if (e.Row.RowIndex % 2 == 0)
{
//if you want to do something to rows 1, 3, 5 etc
}
}
}
数据行有完整的解决方案,即网格视图的标签,但不在数据源的数据绑定中,我们可以在没有 OnRowDataBound 的情况下直接使用。请尽量做到这一点 solution.so 我们有一个例子
在 aspx 页面中:
<asp:GridView ID="GridViewBalance" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="card_no" GridLines="None" CssClass="table table-striped"
OnPageIndexChanging="GridViewBalance_PageIndexChanging" PageSize="50" AllowPaging="True"
CellSpacing="4">
<Columns>
<asp:BoundField DataField="card_no" HeaderText="Card No" />
<asp:BoundField DataField="acc_no" HeaderText="Account No" />
<asp:BoundField DataField="cname" HeaderText="Name" />
<asp:BoundField DataField="mobileno" HeaderText="Mobile No" />
<asp:BoundField DataField="balance" HeaderText="Balance" />
<asp:BoundField DataField="created_date" HeaderText="Creation date" DataFormatString="{0:d/M/yyyy HH:mm:ss}" />
<asp:TemplateField HeaderText="Active-Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" ForeColor="White" Text='<%# GetStatus(Eval("is_active").ToString()) %>' BackColor='<%# GetColor(Eval("is_active").ToString()) %>' ToolTip="Transaction Status"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
隐藏代码:
This is used for the color of the grid view data rows.
public System.Drawing.Color GetColor(string butColor)
{
System.Drawing.Color cr = new System.Drawing.Color();
if (butColor == "1")
{
cr = System.Drawing.Color.Green;
}
else if (butColor == "0")
{
cr = System.Drawing.Color.Red;
}
return cr;
}
This is used for the status:
protected string GetStatus(string butStatus)
{
if (butStatus == "1")
{
butStatus = "ACTIVE";
}
else if (butStatus == "0")
{
butStatus = "DEACTIVE";
}
return butStatus;
}