如何在Cellgridview的Cell离开事件中获得细胞价值? [英] How Can I Get Cell Value On Cell leave Event Of Datagridview?

查看:95
本文介绍了如何在Cellgridview的Cell离开事件中获得细胞价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

I am using following code:

dgvEdu.UpdateCellValue(e.ColumnIndex, e.RowIndex);

                string headerText = dgvEdu.Columns[e.ColumnIndex].HeaderText;
                DataGridViewDataErrorEventArgs ex;

                DataGridViewRow objRow = new DataGridViewRow();
                objRow = dgvEdu.Rows[e.RowIndex];

                if (headerText.Equals("Total Marks"))
                {
                    string strPattern = "^([0-9]{1,7}|[0-9]{1,7}.[0-9]{1,3})$";
                    Regex objrgx = new Regex(strPattern);

                    string cellValue = Convert.ToString(objRow.Cells["Total Marks"].Value);

                    if (objrgx.IsMatch(cellValue) == false)
                    {
                        ex = new DataGridViewDataErrorEventArgs(null, e.ColumnIndex, e.RowIndex, DataGridViewDataErrorContexts.InitialValueRestoration);
                        dgvEdu_DataError(this, ex);
                        dgvEdu.CancelEdit();
                    }

                }



这里我得到了objRow.Cells [Total Marks]。值null。

有人能给我回答为什么我得到空?


Here I am getting objRow.Cells["Total Marks"].Value null.
Can somebody give me answer why I am getting null?

推荐答案

;
Regex objrgx = new 正则表达式(strPattern);

string cellValue = Convert.ToString(objRow.Cells [ 总标记]。值);

if (objrgx.IsMatch(cellValue)== false
{
ex = new DataGridViewDataErrorEventArgs( null ,e.ColumnIndex,e.RowIndex,DataGridViewDataErrorContexts.InitialValueRestoration);
dgvEdu_DataError( this ,ex);
dgvEdu.CancelEdit();
}

}
"; Regex objrgx = new Regex(strPattern); string cellValue = Convert.ToString(objRow.Cells["Total Marks"].Value); if (objrgx.IsMatch(cellValue) == false) { ex = new DataGridViewDataErrorEventArgs(null, e.ColumnIndex, e.RowIndex, DataGridViewDataErrorContexts.InitialValueRestoration); dgvEdu_DataError(this, ex); dgvEdu.CancelEdit(); } }



这里我得到了objRow.Cells [Total Marks]。值null。

有人可以给我回答为什么我得到null吗?


Here I am getting objRow.Cells["Total Marks"].Value null.
Can somebody give me answer why I am getting null?


这是你的代码的修订版。将变量名称更改为有意义。用你的异常处理程序替换throw。



Here's a revision to your code. Changed variable names to be meaningful. Replace the throw with your exception handler.

// *********************************** education_DGV_CellLeave

void education_DGV_CellLeave (
                            object                    sender,
                            DataGridViewCellEventArgs e )
    {

    if ( e.ColumnIndex < 0 )
        {

        }
    else if ( String.IsNullOrEmpty (
                education_DGV.
                    Columns [ e.ColumnIndex ].HeaderText ) )
        {

        }
    else if ( !( education_DGV.
                     Columns [ e.ColumnIndex ].HeaderText.
                                               ToLower ( ).
                                               Trim ( ) ).
                 Equals ( "total marks" ) )
        {

        }
    else if ( e.RowIndex < 0 )
        {

        }
    else
        {
        string              cell_string = String.Empty;
        decimal             cell_value = -1.0M;

        cell_string = education_DGV [ e.RowIndex,
                                      e.ColumnIndex ].
                                          Value.
                                          ToString ( );
        if ( String.IsNullOrEmpty ( cell_string ) )
            {

            }
        else if ( !decimal.TryParse (     cell_string,
                                      out cell_value ) )
            {
            DataGridViewDataErrorEventArgs exception;

            exception = new DataGridViewDataErrorEventArgs (
                                null,
                                e.ColumnIndex,
                                e.RowIndex,
                                DataGridViewDataErrorContexts.
                                    InitialValueRestoration );
            education_DGV.CancelEdit ( );
            throw new DataException (
                String.Format (
                    "Data error  (column {0}, row {1})",
                    e.ColumnIndex,
                    e.RowIndex ) );
            }
        }
    }



希望有所帮助。


Hope that helps.


这篇关于如何在Cellgridview的Cell离开事件中获得细胞价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆