Reputation: 187
This is driving me batty. I have tried every example of this on the site and cannot get it to work. It will not fired the event
RowDataBound="SYSGrid_RowDataBound"
in the Gridview properties<%@ Import Namespace="System.Drawing" %>
on the aspx page, no .cs file for thisThis is the code
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;
}
}
}
I am fairly new at C# so if this is a stupid issue/question then I am willing to take harsh constructive critisism. Thanks in advance.
Upvotes: 0
Views: 9310
Reputation: 470
//.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;
}
}
}
Upvotes: 0
Reputation: 2566
Try this Code
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;
}
}
}
Upvotes: 0
Reputation: 4361
Here is a sample with code within aspx itself. I've added some inline comments. Refer to them to get an understanding. Place it in a aspx page. You should get the following outcome.
<%@ 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>
Upvotes: 3
Reputation: 1899
Make sure your <asp:GridView>
has OnRowDataBound="SYSGrid_RowDataBound"
in the definition and also know that the .Cells[9]
is zero-based.
<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>
Upvotes: 1