INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Conditional formatting and TemplateFields

Conditional formatting and TemplateFields

(OP)
OK, I am getting this error

CODE

Error in execution System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info) at System.Convert.ToInt16(String value) at CreditReports._viewCustomers.gvCredit_RowDataBound(Object sender, GridViewRowEventArgs e) in e:\kt-webs\JBK\CORPORATE\creditreports\ViewCustomersReports.aspx.cs:line 192 at System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.GridView.DataBind() at CreditReports._viewCustomers.BindGridView() in e:\kt-webs\JBK\CORPORATE\creditreports\ViewCustomersReports.aspx.cs:line 121 

and here are the code sections. Line 192 is in this section

CODE

protected void gvCredit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                DropDownList ddlCompany;
                ddlCompany = (DropDownList)e.Row.FindControl("ddlCompany");
                if (!(ViewState["FilterCompany"] == null))
                {
                    ddlCompany.SelectedValue = (string)ViewState["FilterCompany"];
                }
                else
                {
                    ddlCompany.SelectedIndex = 0;
                }
                DropDownList ddlSyspro;
                ddlSyspro = (DropDownList)e.Row.FindControl("ddlSyspro");
                if (!(ViewState["FilterSyspro"] == null))
                {
                    ddlSyspro.SelectedValue = (string)ViewState["FilterSyspro"];
                }
                else
                {
                    ddlSyspro.SelectedIndex = 0;
                }
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Paydex
                int? pdx = Convert.ToInt16(e.Row.Cells[9].Text);  (LINE 192)
                if (pdx == 0)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#FFFFFF"); // This will make row back color white 
                }
                else if (pdx < 55)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 
                }
                else if (pdx < 70)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#FFFF99"); // This will make row back color yellow
                }
                else if (pdx >= 70)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#CCFFCC"); // This will make row back color mintgreen; blue is 99CCFF, c6efce
                }
etc.... 

and then in the bind statement

CODE

cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@FilterCompany", Company);
                cmd.Parameters.AddWithValue("@FilterSyspro", ViewState["FilterSyspro"]);
                cmd.Parameters.AddWithValue("@Year", Year);
                cmd.Parameters.AddWithValue("@Month", Month);
                cmd.Parameters.AddWithValue("@Analyzed", Analyzed);

                cmd.Connection = con;
                sda.SelectCommand = cmd;

                DataSet dsCredit = new DataSet();

                sda.Fill(dsCredit, "Credit");

                DataView dvCredit = dsCredit.Tables["Credit"].DefaultView;
                dvCredit.Sort = ViewState["SortExpression"].ToString();

                gvCredit.DataSource = dvCredit;
                gvCredit.DataBind();     LINE 121
                DropDownList ddlCompany = (DropDownList)gvCredit.HeaderRow.FindControl("ddlCompany");
                this.BindCompanyList(ddlCompany);
                DropDownList ddlSyspro = (DropDownList)gvCredit.HeaderRow.FindControl("ddlSyspro");
                this.BindSysproList(ddlSyspro); 

And then the aspx page

CODE

<asp:BoundField DataField="DBNumber" HeaderText="D&B Number" ReadOnly="True" SortExpression="DBNumber" HeaderStyle-Wrap="false" ItemStyle-Wrap="false" /> 
<asp:BoundField DataField="NextReviewDue" HeaderText="Next Review Due" DataFormatString="{0:M/dd/yyyy}" ReadOnly="True" SortExpression="NextReviewDue" HeaderStyle-Wrap="true" ItemStyle-HorizontalAlign="Center" /> 
<asp:BoundField DataField="DateReportPulled" HeaderText="Date Report Pulled" DataFormatString="{0:M/dd/yyyy}" ReadOnly="True" SortExpression="DateReportPulled" HeaderStyle-Wrap="true" ItemStyle-HorizontalAlign="Center" /> 
<asp:TemplateField HeaderText="Date Analyzed" SortExpression="DateAnalyzed" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate >
        <asp:Label ID="lblDateAnalyzed" runat="server" Text='<%# Bind("DateAnalyzed", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:Checkbox ID="DateAnalyzed" runat="server" Text='<%# Bind("DateAnalyzed", "{0:d}")%>' AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paydex" SortExpression="Paydex" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblPaydex" runat="server" Text='<%# Bind("Paydex") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="Paydex" runat="server" Text='<%# Bind("Paydex") %>' />
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="3 Month Paydex" SortExpression="3MonthPaydex" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lbl3MonthPaydex" runat="server" Text='<%# Bind("3MonthPaydex") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="MonthPaydex" runat="server" Text='<%# Bind("3MonthPaydex") %>' />
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Commercial Credit Class" SortExpression="CommercialCreditClass" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblCommercialCreditClass" runat="server" Text='<%# Bind("CommercialCreditClass") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="CommercialCreditClass" runat="server" Text='<%# Bind("CommercialCreditClass") %>' />
    </EditItemTemplate>
</asp:TemplateField> 

Those are all just snippets, if it would be more helpful to post larger sections let me know, but I think I am likely just missing something simple. What I am doing is simply displaying a recordset in a gridview with conditional formatting like you would do in an Excel spreadhseet for management. It actually worked fine until I changed a number of the fields from asp:Boundfield to asp:TemplateField and now it writes out the first row and then fails with the above error. And I am sure that I am missing something trivial since it was working fine when I was using Boundfields...

Thanks!
Willie

RE: Conditional formatting and TemplateFields

First, you should not be looking at the text of the cells of the grid, look at the underlying data:

In the rowdatabound event

CODE

DataRowView dataitem = (DataRowView)e.Row.DataItem;
//then get the data like this:
var something = dataitem("your datafield name here"); 

Second, did you debug and step through your code to find the offending value? Somewhere in there you don't have a valid int16

However, if you use the method above instead of using the text, it may fix the issue. If not, you will still have to step through each item to find the bad value.

RE: Conditional formatting and TemplateFields

(OP)
OK, so if in this section of code

CODE

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Paydex
                int? pdx = Convert.ToInt16(e.Row.Cells[9].Text);
                if (pdx == 0)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#FFFFFF"); // This will make row back color white 
                }
                else if (pdx < 55)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 
                }
                else if (pdx < 70)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#FFFF99"); // This will make row back color yellow
                }
                else if (pdx >= 70)
                {
                    e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#CCFFCC"); // This will make row back color mintgreen; blue is 99CCFF, c6efce
                } 

the field name is Paydex, what exactly would that look like? I have learned (somewhat, really am learning) .net/C# thru reading and searching, but there are those areas that I have not found exactly what I am searching for, so if you could help me on this one, I would be very thankful.

Thanks!
Willie

RE: Conditional formatting and TemplateFields


CODE

//First Line in the RowDataBound Event
DataRowView dataitem = (DataRowView)e.Row.DataItem;
var pdx = dataitem("Paydex");

// if statement here.... 

RE: Conditional formatting and TemplateFields

(OP)
Really, it's that easy? The power of knowledge. Thanks, I will give it a show! Were I to search for this, how would you search, what wording would you use?

Thanks,
Willie

RE: Conditional formatting and TemplateFields

It's all trial and error in programming, the more mistakes you make, the more you learn.
Just do it the way you know how, or find online and take it from there.

The code I showed you I learned by looking at other's code, that is also one of the best ways to learn(as long at it is good code)

And of course Google is the best. But when you can't find it, that's what these forums are for. :)

RE: Conditional formatting and TemplateFields

(OP)
Awesome, thanks! BTW, one thing, [] instead of () to index the variable, right?

RE: Conditional formatting and TemplateFields

Yes
I get it confused because I bounce between VB and C# all the time..

good catch :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close