0

以下是我的代码,其中我将列声明为私有字符串,并且我正在使用该值将值绑定到网格但我收到错误输入字符串在“PolicyRenewalGracePeriodDays”附近的格式不正确..请参阅突出显示的文本请在下面告诉我

/// </summary>
private const string COL_UNDERWRITER_DISPLAY_NAME = "UnderwriterDisplayName";
/// <summary>
/// 
/// </summary>
private const string COL_UNDERWRITER_INITIALS = "UnderwriterInitials";
/// <summary>
/// 
/// </summary>
private const string COL_UA_DISPLAY_NAME = "UADisplayName";
/// <summary>
/// 
/// </summary>
private const string COL_UA_INITIALS = "UA";
**private const string COL_RENEWAL_GRACE_PERIOD_DAYS = "PolicyRenewalGracePeriodDays";**
#endregion


    protected void grdAction_DataBound(object sender, EventArgs e)
        {
            foreach (UltraGridRow row in this.grdAction.DisplayLayout.Rows)
            {
                TemplatedColumn col;
                CellItem item;
                HyperLink docLink;
                HyperLink letterLink;
                HyperLink actionLink;
                Label actionLabel;
                var policyClassId = Utility.GetCurrentPolicyClassId();
             PolicyClass policyClass = Utility.GetCurrentPolicyClassEntity();
                var accountId = (int) row.DataKey;
                var insuredName = row.Cells.FromKey(COL_INSURED_NAME_HIDDEN).Text;
                var referenceNumber = row.Cells.FromKey(COL_REFERENCE_NUMBER).Text;
                var statusId = int.Parse(row.Cells.FromKey(COL_STATUS_ID).Text);

                var optionNames = string.Empty;
                if (!string.IsNullOrEmpty(row.Cells.FromKey(COL_OPTION_NAMES).Text))
                    optionNames = row.Cells.FromKey(COL_OPTION_NAMES).Text;

                var optionCount = int.Parse(row.Cells.FromKey(COL_OPTION_COUNT).Text);
                var isVoidable = (row.Cells.FromKey(COL_IS_VOIDABLE).Text == "1");
                bool renewalFlag;
                bool doNotRenewFlag;
                bool hasRenewingReferenceNumber;
                var currentUser = (User) Session[AppConstants.SK_CURRENT_USER];
                var expirationDate = DateTime.MinValue;
                bool convertedFlag;
                var documentCount = int.Parse(row.Cells.FromKey(COL_DOCUMENT_COUNT).Text);
                var allowAddLayer = bool.Parse(row.Cells.FromKey(COL_ALLOW_ADD_LAYER).Text);
                var renewableLayers = row.Cells.FromKey(COL_RENEWABLE_LAYERS).Text;
                int renewalGracePeriodDays = 0;

                **renewalGracePeriodDays = int.Parse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text);**
4

4 回答 4

4

很可能row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text没有返回您所期望的。如果它有 0-9 以外的任何值或大于int.MaxValue你的值,你会得到一个异常。另外nullSystem.String.Empty将导致异常。

您可以改用TryParse它将返回一个布尔值,指示解析是否有效。如果有效,您传入的 int 将设置为您传入的字符串。

给出一些实际的代码;

if(!int.TryParse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text, out renewalGracePeriodDays))
         renewalGracePeriod = MyDefaultValue;
于 2012-11-16T21:49:01.520 回答
2

除了上述答案之外,您还可以轻松创建一个小扩展方法来优雅地处理这些情况:

public static class ExtensionUtils
{
    public static int ToZeroIfNotInt(this string valueToConvert)
    {
        int number =0;
        int.TryParse(valueToConvert,out number);
        return number;
    }
}

然后这样称呼它:

renewalGracePeriodDays = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text.ToZeroIfNotInt();
于 2012-11-16T21:56:19.357 回答
2

如果单元格不包含有效数字(例如空字符串)会怎样?
你得到你的问题中提到的例外。
一个简单的解决方法是使用TryParse方法

int renewalGracePeriodDays;
string temp = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text;
Int32.TryParse(temp, out renewalGracePeriodDays);

来自 MSDN 文档

当此方法返回时,如果转换成功,[第二个参数]包含与 s [第一个参数]中包含的数字等效的 32 位有符号整数值,如果转换失败,则为零。如果 s 参数为空、格式不正确或表示小于 MinValue 或大于 MaxValue 的数字,则转换失败。此参数未初始化传递

我添加了方括号中的粗体和文本。因此,如果您的默认值应为零,则您不必对 TryParse 方法的结果进行任何测试。

于 2012-11-16T21:48:22.453 回答
1

It's possible that the column you are checking is either an empty string, or contains non-integer data. I recommend using the TryParse method.

int renewalGracePeriodDays;
if (!int.TryParse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text), out renewalGracePeriodDays)
{
    renewalGracePeriodDays = 0;
    // Inside here, you can log the exception, alert the user, or end processing
}

If the TryParse fails, your grace period will default to 0. It's a good idea to use this method when dealing with user inputs, because there's no telling what people will enter, even when properly prompted...

于 2012-11-16T22:12:19.600 回答