Hi Guys, I've been trying to display rows in a gridview in different colours depending a condition. At the moment the code is iterating through each row and assessing each of the conditions but builds colours all the rows in the gridview the color of the
first condition assessed. What I need it to do is color the individual rows in the color of the condition in the row, see code below.
I would handle the RowDataBound event and do the color logic there. You would use the e.Row properties as needed. You can get at the underlying data object via the DataItem property, and you can set the color via BackColor.
MetalAsp.Net is correct, you should attempt this in the RowDataBound handler and access the DataItem of the row being bound and base your condition on it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//access the row from your DataItem
AsbestosDataSet.LocationSpecificDataTableRow row = (AsbestosDataSet.LocationSpecificDataTableRow)e.Row.DataItem;
//now base your condition on this rows value.
switch (row.ASB_PRSC_CODE)
{
case "NONE":
e.Row.BackColor = System.Drawing.Color.White;
break;
case "PRESUMED":
e.Row.BackColor = System.Drawing.Color.Yellow;
break;
case "STRONGLY":
e.Row.BackColor = System.Drawing.Color.Orange;
break;
case "IDENTIFIED":
e.Row.BackColor = System.Drawing.Color.Red;
break;
}
}
}
what you can do is save 'row.ASB_PRSC_CODE' value in one hidden field which is in gridview for every row. Now in row_Databound event you can find this value and based on this value and on ur condition you can change color of row.
Dragonfly_X
Member
11 Points
54 Posts
Change GridView row color based on condition
Jun 15, 2009 11:26 AM|LINK
Hi Guys, I've been trying to display rows in a gridview in different colours depending a condition. At the moment the code is iterating through each row and assessing each of the conditions but builds colours all the rows in the gridview the color of the first condition assessed. What I need it to do is color the individual rows in the color of the condition in the row, see code below.
protected void Page_Load(object sender, EventArgs e) { string ASSID = "12345"; string asbestosStatus = null; AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter adapter = new AsbestosInformationForm.AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter(); AsbestosDataSet.LocationSpecificDataTableDataTable locationTable = new AsbestosDataSet.LocationSpecificDataTableDataTable(); locationTable = adapter.GetPropertySpecificData(ASSID); foreach (AsbestosDataSet.LocationSpecificDataTableRow row in locationTable) { asbestosStatus = row.ASB_PRSC_CODE; switch (asbestosStatus) { case "NONE": GridView1.RowStyle.BackColor = System.Drawing.Color.White; break; case "PRESUMED": GridView1.RowStyle.BackColor = System.Drawing.Color.Yellow; break; case "STRONGLY": GridView1.RowStyle.BackColor = System.Drawing.Color.Orange; break; case "IDENTIFIED": GridView1.RowStyle.BackColor = System.Drawing.Color.Red; break; } } GridView1.DataSource = locationTable; GridView1.AutoGenerateSelectButton = true; GridView1.DataBind(); }gridview condition Based row formatting
MetalAsp.Net
All-Star
112229 Points
18266 Posts
Moderator
Re: Change GridView row color based on condition
Jun 15, 2009 12:10 PM|LINK
I would handle the RowDataBound event and do the color logic there. You would use the e.Row properties as needed. You can get at the underlying data object via the DataItem property, and you can set the color via BackColor.
meetmanthan
Member
248 Points
79 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 12:18 PM|LINK
Write your condition in girdView's Row created event like this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
asbestosStatus = row.ASB_PRSC_CODE;
switch (asbestosStatus)
{
case "NONE":
GridView1.RowStyle.BackColor = System.Drawing.Color.White;
break;
case "PRESUMED":
GridView1.RowStyle.BackColor = System.Drawing.Color.Yellow;
break;
case "STRONGLY":
GridView1.RowStyle.BackColor = System.Drawing.Color.Orange;
break;
case "IDENTIFIED":
GridView1.RowStyle.BackColor = System.Drawing.Color.Red;
break;
}
}
Manthan Upadhyay.
Make it "answer" if it helps you to solve ur query.
Dragonfly_X
Member
11 Points
54 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 12:54 PM|LINK
Hi Guys, Thanks a mil! Still colors all the rows red, which is the first condition it assesses.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { string asbestosStatus = null; AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter adapter = new AsbestosInformationForm.AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter(); AsbestosDataSet.LocationSpecificDataTableDataTable locationTable = new AsbestosDataSet.LocationSpecificDataTableDataTable(); locationTable = adapter.GetPropertySpecificData(Session["ASSID"].ToString()); if (e.Row.RowType == DataControlRowType.DataRow) { foreach (AsbestosDataSet.LocationSpecificDataTableRow row in locationTable) { asbestosStatus = row.ASB_PRSC_CODE; switch (asbestosStatus) { case "NONE": e.Row.BackColor = System.Drawing.Color.White; break; case "PRESUMED": e.Row.BackColor = System.Drawing.Color.Yellow; break; case "STRONGLY": e.Row.BackColor = System.Drawing.Color.Orange; break; case "IDENTIFIED": e.Row.BackColor = System.Drawing.Color.Red; break; } } }meetmanthan
Member
248 Points
79 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:01 PM|LINK
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
string asbestosStatus = null;
AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter adapter = new
AsbestosInformationForm.AsbestosDataSetTableAdapters.LocationSpecificDataTableAdapter();
AsbestosDataSet.LocationSpecificDataTableDataTable locationTable = new AsbestosDataSet.LocationSpecificDataTableDataTable();
locationTable = adapter.GetPropertySpecificData(Session["ASSID"].ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (AsbestosDataSet.LocationSpecificDataTableRow row in locationTable)
{
asbestosStatus = row.ASB_PRSC_CODE;
switch (asbestosStatus)
{
case "NONE":
e.Row.BackColor = System.Drawing.Color.White;
break;
case "PRESUMED":
e.Row.BackColor = System.Drawing.Color.Yellow;
break;
case "STRONGLY":
e.Row.BackColor = System.Drawing.Color.Orange;
break;
case "IDENTIFIED":
e.Row.BackColor = System.Drawing.Color.Red;
break;
}
}
}
Manthan Upadhyay.
Make it "answer" if it helps you to solve ur query.
Dragonfly_X
Member
11 Points
54 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:13 PM|LINK
Meet ??? It's the same code I posted whiich still colors all the rows the same color.
alessandro
Contributor
6800 Points
1105 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:19 PM|LINK
MetalAsp.Net is correct, you should attempt this in the RowDataBound handler and access the DataItem of the row being bound and base your condition on it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//access the row from your DataItem
AsbestosDataSet.LocationSpecificDataTableRow row = (AsbestosDataSet.LocationSpecificDataTableRow)e.Row.DataItem;
//now base your condition on this rows value.
switch (row.ASB_PRSC_CODE)
{
case "NONE":
e.Row.BackColor = System.Drawing.Color.White;
break;
case "PRESUMED":
e.Row.BackColor = System.Drawing.Color.Yellow;
break;
case "STRONGLY":
e.Row.BackColor = System.Drawing.Color.Orange;
break;
case "IDENTIFIED":
e.Row.BackColor = System.Drawing.Color.Red;
break;
}
}
}
meetmanthan
Member
248 Points
79 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:24 PM|LINK
ok.
what you can do is save 'row.ASB_PRSC_CODE' value in one hidden field which is in gridview for every row. Now in row_Databound event you can find this value and based on this value and on ur condition you can change color of row.
Like this:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="EmpID" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" OnPageIndexChanged="GridView1_PageIndexChanged"
PageSize="5" onrowcreated="GridView1_RowCreated">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnval" Value ="123" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" ReadOnly="True" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="PhNo" HeaderText="PhNo" SortExpression="PhNo" />
<asp:BoundField DataField="EmailId" HeaderText="EmailId" SortExpression="EmailId" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
now on rowdatabound event put condition like:
if(((HiddenFiled)e.row.findControl('hdnval')).value == '123')
e.row.BackColor = red;
else if(((HiddenFiled)e.row.findControl('hdnval')).value == '123')
e.row.BackColor = black;
Manthan Upadhyay.
Make it "answer" if it helps you to solve ur query.
Dragonfly_X
Member
11 Points
54 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:35 PM|LINK
Thanks Alessandro, I think I know where I went wrong. the only thing it's doing now is giving a casting error on the line of code you added.
Unable to cast object of type 'System.Data.DataRowView' to type 'LocationSpecificDataTableRow'
alessandro
Contributor
6800 Points
1105 Posts
Re: Change GridView row color based on condition
Jun 15, 2009 01:40 PM|LINK
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView)e.Row.DataItem;
string asbestosStatus = (string)row["ASB_PRSC_COD"];
switch (asbestosStatus)
{
case "NONE":
e.Row.BackColor = System.Drawing.Color.White;
break;
case "PRESUMED":
e.Row.BackColor = System.Drawing.Color.Yellow;
break;
case "STRONGLY":
e.Row.BackColor = System.Drawing.Color.Orange;
break;
case "IDENTIFIED":
e.Row.BackColor = System.Drawing.Color.Red;
break;
}
}
}