You should be able to do that in "gvPerson_RowDataBound"
As mentioned, but you should check the dataitem and then do a comparision if the data exists:
DataRowView d = ((DataRowView)(e.Row.DataItem));
if (!string.IsNullOrEmpty(d("Terminated"))) {
// Perform logic here
if (bool.Parse(d("Terminated"))) {
// Change the row here...
}
}
Check out my website: http://www.TheTradeBox.com
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
You should be able to do that in "gvPerson_RowDataBound"
As mentioned, but you should check the dataitem and then do a comparision if the data exists:
DataRowView d = ((DataRowView)(e.Row.DataItem)); if (!string.IsNullOrEmpty(d("Terminated"))) { // Perform logic here if (bool.Parse(d("Terminated"))) { // Change the row here... } }
I get an error: 'd' is a 'variable' but is used like a 'method'
Also, I need help where you indicated 'Perform logic here' and 'Change row here'
khurj01
Member
16 Points
241 Posts
Conditionally Change Row Color of a GridView
Jul 17, 2012 09:43 PM|LINK
{ if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) || (e.Row.Cells[4].Text != " ")) { bool result = Convert.ToBoolean(e.Row.Cells[4].Text); if (result == true) e.Row.BackColor = System.Drawing.Color.Aqua; else if (result ==false) e.Row.BackColor = System.Drawing.Color.Cornsilk; }<asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" onpageindexchanging="gvPerson_PageIndexChanging" onrowcancelingedit="gvPerson_RowCancelingEdit" onrowdatabound="gvPerson_RowDataBound" onrowdeleting="gvPerson_RowDeleting" onrowediting="gvPerson_RowEditing" onrowupdating="gvPerson_RowUpdating" onsorting="gvPerson_Sorting" Width="635px" Height="110px" Visible="False"> <RowStyle BackColor="White" ForeColor="#003399" /> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID" /> <asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="HireWorked" SortExpression="HireWorked"> <EditItemTemplate> <asp:TextBox ID="TextBox2a" CssClass="clDate" runat="server" Text='<%# Bind("HireWorked") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2a" runat="server" Text='<%# Bind("HireWorked") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <!-- Boolean --> <asp:BoundField DataField="Terminated" HeaderText="Terminated" ReadOnly="True" SortExpression="Terminated" /> <asp:BoundField DataField="TerminatedBy" HeaderText="TerminatedBy" ReadOnly="True" SortExpression="TerminatedBy" /> </Columns> </asp:GridView>avidyarthi
Participant
1745 Points
372 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 09:53 PM|LINK
You should be able to do that in "gvPerson_RowDataBound"
mebinici
Participant
815 Points
256 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 09:59 PM|LINK
As mentioned, but you should check the dataitem and then do a comparision if the data exists:
DataRowView d = ((DataRowView)(e.Row.DataItem)); if (!string.IsNullOrEmpty(d("Terminated"))) { // Perform logic here if (bool.Parse(d("Terminated"))) { // Change the row here... } }Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
khurj01
Member
16 Points
241 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:04 PM|LINK
I get an error: 'd' is a 'variable' but is used like a 'method'
Also, I need help where you indicated 'Perform logic here' and 'Change row here'
avidyarthi
Participant
1745 Points
372 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:09 PM|LINK
you can also try something like this:
protected void gvPerson_RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { if(e.Row.RowIndex == 0) // This is row no.1 if(e.Row.Cells[0].Text == "XXX") e.Row.Cells[0].BackColor = Color.Red; if(e.Row.RowIndex == 1) // This is row no.2 if(e.Row.Cells[0].Text == "YYY") e.Row.Cells[0].BackColor = Color.Green; } }khurj01
Member
16 Points
241 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:12 PM|LINK
This will not help if I have many rows.
mebinici
Participant
815 Points
256 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:17 PM|LINK
Where is your datasource?
You'll need to an object container for the dataitems.
Are you binding to a SqlDataSource or using some business logic?
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
avidyarthi
Participant
1745 Points
372 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:19 PM|LINK
You can remove the index comparison and just use your comparison.
khurj01
Member
16 Points
241 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:25 PM|LINK
I removed the index and it changed the color of my columns; not the entire row.
if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[3].Text == "True") e.Row.Cells[3].BackColor = Color.Red; if (e.Row.Cells[3].Text == "False") e.Row.Cells[3].BackColor = Color.Green; }khurj01
Member
16 Points
241 Posts
Re: Conditionally Change Row Color of a GridView
Jul 17, 2012 10:47 PM|LINK
Thanks to all; I have figured it using the code you provide:
if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[3].Text == "True") { e.Row.BackColor = System.Drawing.Color.Red; } if (e.Row.Cells[3].Text == "False") { e.Row.BackColor = System.Drawing.Color.LightCyan; } }