简体   繁体   English

C#Winform:验证GridDataView单元格值时出现Null异常错误

[英]C# Winform: Null Exception Error when Validating GridDataView Cell Value

I'm trying to validate the value of a cell to ensure that it's numeric. 我正在尝试验证单元格的值,以确保它是数字。 If it isn't I want to display an error, wipe the cell, and set them back into the cell to correct. 如果不是,我想显示错误,请擦除单元格,然后将它们重新设置到单元格中以进行更正。

    private void gridData_CellEndEdit(object sender, DataGridViewCellEventArgs e) {

        // If Cell Edited is in the Add'l Qty Column
        if (gridData.Columns[e.ColumnIndex].Name == "AddlQty") {
            int intVal;

            // Validate Entry for Numeric Only
            if (int.TryParse(gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out intVal)) {
                CalcFinalQty(e.RowIndex);
            } else {
                // Clear and Send User Back to Try Again
                gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                gridData.CurrentCell = gridData.Rows[e.RowIndex].Cells[e.ColumnIndex];
                MessageBox.Show("Entry Not Valid, Only Numeric Values Accepted");
                gridData.BeginEdit(true);
            }
        }
    } // End gridData_CellEndEdit

It catches if it's numeric or not, wipes the cell, but displays the MessageBox twice. 它捕获是否为数字,擦除单元格,但显示两次MessageBox。 After the first time, the selected cell moves to the next cell down, it pops up another MessageBox, and THEN moves back to the cell and set to edit. 第一次之后,选定的单元格向下移动到下一个单元格,它弹出另一个MessageBox,然后THEN移回该单元格并设置为编辑。

If you hit Enter without entering anything into the cell, it gives a Null exception error. 如果您在不输入任何内容的情况下按Enter键,则会出现Null异常错误。

Try assigning a "0" value instead of clearing. 尝试分配一个“ 0”值而不是清除。

gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "0";

When execute gridData.BeginEdit(true); 当执行gridData.BeginEdit(true); it goes back to the edit event and will goes to each cell and Try.Parse part is called again. 它返回到编辑事件,并将转到每个单元格,并再次调用Try.Parse部分。

Try.Parse is getting false because it can't parse "" value to int . Try.Parse变得假,因为它无法将""值解析为int

This ensures that your application will force to put zero value when user didn't put anything. 这样可以确保您的应用程序在用户未放置任何内容时将强制放置零值。

I have a suspicion here. 我在这里怀疑。 I think beginEdit is triggering somehow the event the second time. 我认为beginEdit会以某种方式触发事件。 If that's the case can you double check in your else statement that the value is not empty and if it's not, then show message box? 如果是这样,您能否再次检查else语句以确保该值不为空,如果不是,则显示消息框? it'd be something like : 就像这样:

private void gridData_CellEndEdit(object sender, DataGridViewCellEventArgs e) {

    // If Cell Edited is in the Add'l Qty Column
    if (gridData.Columns[e.ColumnIndex].Name == "AddlQty") {
        int intVal;

        // Validate Entry for Numeric Only
        if (int.TryParse(gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out intVal)) {
            CalcFinalQty(e.RowIndex);
        } else {
            if(!String.IsNullOrEmpty(gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
            // Clear and Send User Back to Try Again
            gridData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
            gridData.CurrentCell = gridData.Rows[e.RowIndex].Cells[e.ColumnIndex];
            MessageBox.Show("Entry Not Valid, Only Numeric Values Accepted");
            gridData.BeginEdit(true);
        }
    }
}

Let me know if it helps, cheers. 让我知道是否有帮助,欢呼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM