Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using following code:
C#
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();
                    }

                }

Here I am getting objRow.Cells["Total Marks"].Value null.
Can somebody give me answer why I am getting null?
Posted
Updated 19-Sep-14 20:04pm
v3
Comments
George Jonsson 20-Sep-14 2:33am    
Cannot tell you much coz I know nothing about your grid view. Is the column name correct?
You need to use the debugger and check the value of objRow.Cells[e.ColumnIndex]
gggustafson 20-Sep-14 13:00pm    
Ate you trying to match a decimal point? If so, use \. in the pattern.

1 solution

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

C#
// *********************************** 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900