这让我很沮丧。我已经在网站上尝试过这方面的每一个例子,但无法让它发挥作用。它不会触发事件
RowDataBound="SYSGrid_RowDataBound"
<%@ Import Namespace="System.Drawing" %>
,此这是代码
protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[9].Text == "Missing")
{
e.Row.Cells[9].BackColor = Color.Red;
e.Row.Cells[9].ForeColor = Color.White;
}
}
}
我在C#上相当新,所以如果这是一个愚蠢的问题/问题,那么我愿意采取严厉的建设性批评。提前谢谢。
答案 0 :(得分:2)
以下是aspx本身内部代码的示例。我添加了一些内联评论。请参阅它们以获得理解。将它放在一个aspx页面中。你应该得到以下结果。
<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Doing the binding when the page is loading for the first time (not on postbacks)
if (!IsPostBack)
{
//Test datasource (Creating a datatable with 10 columns. Then adding 3 rows. cell indeces are 0 based.)
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("col1");
DataColumn dc2 = new DataColumn("col2");
DataColumn dc3 = new DataColumn("col3");
DataColumn dc4 = new DataColumn("col4");
DataColumn dc5 = new DataColumn("col5");
DataColumn dc6 = new DataColumn("col6");
DataColumn dc7 = new DataColumn("col7");
DataColumn dc8 = new DataColumn("col8");
DataColumn dc9 = new DataColumn("col9");
DataColumn dc10 = new DataColumn("col10");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
dt.Columns.Add(dc8);
dt.Columns.Add(dc9);
dt.Columns.Add(dc10);
//Second row index 9 has "Missing" as the text
dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });
dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "Missing" });
dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });
//Set datasource. Then bind it. (here the grid is using auto generated columns)
SYSGrid.DataSource = dt;
SYSGrid.DataBind();
}
}
protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[9].Text == "Missing")
{
e.Row.Cells[9].BackColor = Color.Red;
e.Row.Cells[9].ForeColor = Color.White;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview runat="server" ID="SYSGrid" OnRowDataBound="SYSGrid_RowDataBound"></asp:gridview>
</div>
</form>
</body>
</html>
答案 1 :(得分:1)
确保您的<asp:GridView>
在定义中有OnRowDataBound="SYSGrid_RowDataBound"
,并且知道.Cells[9]
从零开始。
<asp:GridView runat="server"
ID="SYSGrid"
AutoGenerateColumns="false"
OnRowDataBound="SYSGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="Column0" HeaderText="Column0" />
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
<asp:BoundField DataField="Column4" HeaderText="Column4" />
<asp:BoundField DataField="Column5" HeaderText="Column5" />
<asp:BoundField DataField="Column6" HeaderText="Column6" />
<asp:BoundField DataField="Column7" HeaderText="Column7" />
<asp:BoundField DataField="Column8" HeaderText="Column8" />
<asp:BoundField DataField="Column9" HeaderText="Column9" />
</Columns>
</asp:GridView>
答案 2 :(得分:0)
试用此代码
protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Convert which control you use
Label lblcol = (Label) e.Row.findcontrol("yourcolumnname") ;
if (lblcol.Text == "Missing")
{
e.Row.Cells[9].BackColor = Color.Red;
e.Row.Cells[9].ForeColor = Color.White;
}
}
}
答案 3 :(得分:0)
//.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
ondatabound="GridView1_DataBound" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="ParentID" HeaderText="ParentID"
SortExpression="ParentID" />
<asp:BoundField DataField="Content" HeaderText="Content"
SortExpression="Content" />
<asp:BoundField DataField="ShortContent" HeaderText="ShortContent"
SortExpression="ShortContent" />
//Change the Status Cell Color-----
<asp:TemplateField HeaderText="Status" ControlStyle-Width="75px" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
//--------
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="App_Data/Database1.accdb" SelectCommand="SELECT CMSMenus.ID, CMSMenus.Title, CMSMenus.Location, CMSMenus.ParentID, CMSMenus.Content, CMSMenus.ShortContent
FROM CMSMenus;
">
</asp:AccessDataSource>
//C#
protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int i =0 ; i <= GridView1.Rows.Count -1 ;i++)
{
Label lblparent = (Label)GridView1.Rows[i].FindControl("Label1");//Get ParentID
if (lblparent.Text == "1")
{
GridView1.Rows[i].Cells[6].BackColor = Color.Yellow;
lblparent.ForeColor = Color.Black;
}
else
{
GridView1.Rows[i].Cells[6].BackColor = Color.Green;
lblparent.ForeColor = Color.Yellow;
}
}
}