Trying to make a DataGridView field a color if a condition is met.
-
Getting an error: input string was not in a correct format asp: onrowdatabound="highlightrow"
protected void highlightrow(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int ActualFTE=Convert.ToInt32(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)
{
e.Row.BackColor = Color.Red;
}
}
} -
Getting an error: input string was not in a correct format asp: onrowdatabound="highlightrow"
protected void highlightrow(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int ActualFTE=Convert.ToInt32(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)
{
e.Row.BackColor = Color.Red;
}
}
}The error message says it clearly. You are trying to covert a decimal to int. Change your stament like below and try.
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25) -
The error message says it clearly. You are trying to covert a decimal to int. Change your stament like below and try.
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)Duh! Thanks.
-
The error message says it clearly. You are trying to covert a decimal to int. Change your stament like below and try.
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)Still getting the same error. :(
-
Still getting the same error. :(
Can you post what value is coming in
e.Row.Cells[5].Text
It will give a better idea on what we are dealing with.
-
Can you post what value is coming in
e.Row.Cells[5].Text
It will give a better idea on what we are dealing with.
the count is 9.
-
Can you post what value is coming in
e.Row.Cells[5].Text
It will give a better idea on what we are dealing with.
sorry I meant 0.9500
-
sorry I meant 0.9500
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)In the above code check whether
e.Row.Cells[5].Text
returns data that can be converted to decimal. Or add ToString() at the end and see if it helps.decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text.ToString());
-
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)In the above code check whether
e.Row.Cells[5].Text
returns data that can be converted to decimal. Or add ToString() at the end and see if it helps.decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text.ToString());
Same error: Input string was not in a correct format.
-
Same error: Input string was not in a correct format.
Just to make sure everything is fine, try hardcoding the value and see if you still get the error.
decimal ActualFTE=Convert.ToDecimal("0.9500");
If this works without exception, then you can be sure, the problem is with the expression
e.Row.Cells[5].Text
. This expression you use should return some string (like your value 0.9500) that can be converted to decimal for the conversion to work without exception. -
Just to make sure everything is fine, try hardcoding the value and see if you still get the error.
decimal ActualFTE=Convert.ToDecimal("0.9500");
If this works without exception, then you can be sure, the problem is with the expression
e.Row.Cells[5].Text
. This expression you use should return some string (like your value 0.9500) that can be converted to decimal for the conversion to work without exception.okay it didn't error this time but nothing changed.
-
okay it didn't error this time but nothing changed.
May be the value ActualFTE is not greater than 1.25, as per your condition.
if (ActualFTE > 1.25) { e.Row.BackColor = Color.Red; }
So the color will not be changed.
-
May be the value ActualFTE is not greater than 1.25, as per your condition.
if (ActualFTE > 1.25) { e.Row.BackColor = Color.Red; }
So the color will not be changed.
That is true for most of the 251 rows. I only see a couple that are over 1.25. So I should take out that code?
-
May be the value ActualFTE is not greater than 1.25, as per your condition.
if (ActualFTE > 1.25) { e.Row.BackColor = Color.Red; }
So the color will not be changed.
Do you think I should post all of my code to see if I have something missing?
-
Do you think I should post all of my code to see if I have something missing?
When you want the row to be colored? Change the if condition based on that. It should work.
-
When you want the row to be colored? Change the if condition based on that. It should work.
Not the whole row only that field. ActualFTE.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;namespace StaffingWebParts.CatwComment
{
public partial class CatwCommentUserControl : UserControl
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
SqlCommand cmd = new SqlCommand();protected void Page\_Load(object sender, EventArgs e) { lblMsg.Text = ""; if (!Page.IsPostBack) { BindSubjectData(); } } protected void highlightrow(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { decimal ActualFTE = Convert.ToDecimal(e.Row.Cells\[5\].Text.ToString()); if (ActualFTE > 1.2500M) { e.Row.BackColor = Color.Red; } } } //call to bind gridview protected void BindSubjectData() { using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = ("select a.id, b.StaffTrackingID, a.ResourceName, b. EstimateHours, EstimateFTE, b.ActualHours, b.ActualFTE,b.Comment, b.CommentBy, b.Period from StaffTracking a, StaffTrackingFTEData b where a.id = b.StaffTrackingid order by ResourceName"); cmd.Connection = sqlCon; sqlCon.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { gvCATW.DataSource = dt; gvCATW.DataBind(); } else { DataRow dr = dt.NewRow(); dt.Rows.Add(dr); gvCATW.DataSource = dt; gvCATW.DataBind(); gvCATW.Rows\[0\].Visible = false;
-
Not the whole row only that field. ActualFTE.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;namespace StaffingWebParts.CatwComment
{
public partial class CatwCommentUserControl : UserControl
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
SqlCommand cmd = new SqlCommand();protected void Page\_Load(object sender, EventArgs e) { lblMsg.Text = ""; if (!Page.IsPostBack) { BindSubjectData(); } } protected void highlightrow(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { decimal ActualFTE = Convert.ToDecimal(e.Row.Cells\[5\].Text.ToString()); if (ActualFTE > 1.2500M) { e.Row.BackColor = Color.Red; } } } //call to bind gridview protected void BindSubjectData() { using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = ("select a.id, b.StaffTrackingID, a.ResourceName, b. EstimateHours, EstimateFTE, b.ActualHours, b.ActualFTE,b.Comment, b.CommentBy, b.Period from StaffTracking a, StaffTrackingFTEData b where a.id = b.StaffTrackingid order by ResourceName"); cmd.Connection = sqlCon; sqlCon.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { gvCATW.DataSource = dt; gvCATW.DataBind(); } else { DataRow dr = dt.NewRow(); dt.Rows.Add(dr); gvCATW.DataSource = dt; gvCATW.DataBind(); gvCATW.Rows\[0\].Visible = false;
Refere this question for changing the color of a grid row based on some condition. The code is in Vb.Net but you can get the idea. How Can I Change A Rows Backcolour Based On The Value In The First Cell Of The Same Row[^]
-
Refere this question for changing the color of a grid row based on some condition. The code is in Vb.Net but you can get the idea. How Can I Change A Rows Backcolour Based On The Value In The First Cell Of The Same Row[^]
Mathi, I was able to get it to work. Thank you for your help and patience. You and others have help me to complete this project.
protected void gvCATW_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{// To check condition on integer value if (Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ActualFTE")) > 1.25M) { e.Row.BackColor = System.Drawing.Color.Red; }
}