Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional formatting and TemplateFields

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
0
0
US
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
 
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.
 
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
 

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

// if statement here....
 
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
 
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. :)
 
Awesome, thanks! BTW, one thing, [] instead of () to index the variable, right?
 
Yes
I get it confused because I bounce between VB and C# all the time..

good catch :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top