This is a migrated thread and some comments may be shown as answers.

Error while updating GridViewCell Value

2 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
yaqub
Top achievements
Rank 2
yaqub asked on 20 Aug 2009, 06:37 AM

 

Hi,

 

I am displaying customer data [Id, Name and Address] in grid view. I also need to display some calculated value “Customer Points” in the grid. For the “Customer Points” I have added a GridViewTextBoxColumn “txtCustomerPoints” to the grid.

 

 

 

For displaying data in “txtCustomerPoints” i am using the CellFormatting Event as below:

 

        private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)

        {

            try

            {

                string columnUniqueName = ((GridViewDataColumn)e.CellElement.ColumnInfo).UniqueName;

                if (columnUniqueName == "txtCustomerPoints " )

                {

                    if (IsColumnExistsInGrid(RadGridView1.MasterGridViewTemplate, columnUniqueName))

                    {

                        RadGridView1.GridElement.BeginUpdate();

                        e.CellElement.RowInfo.Cells[columnUniqueName].Value = "Some Calculated Value";

                        RadGridView1.GridElement.EndUpdate();

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

 

But this generate an error on this line:

e.CellElement.RowInfo.Cells[columnUniqueName].Value = "Some Calculated Value";

 

The error is:

 

ex = {"Object reference not set to an instance of an object."}               

 

StackTrace       "   at MSMainPrj.FormsEmbedded.Booking. RadGridView1_ValueChanging(Object sender, ValueChangingEventArgs e) in D:\\Users\\Yaqub.Ahmad\\CS2\\trunk\\code\\MSMainPrj\\FormsEmbedded\\Booking.cs:line 1755\r\n   at Telerik.WinControls.UI.RadGridView.OnValueChanging(Object sender, ValueChangingEventArgs e)\r\n   at Telerik.WinControls.UI.RadGridView.CallValueChanging(Object sender, ValueChangingEventArgs e)\r\n   at Telerik.WinControls.UI.GridViewCellInfo.set_Value(Object value)\r\n   at MSMainPrj.Libraries.TelerikGridHelper.SetGridColumnValue(RadGridView grid, CellFormattingEventArgs e, String columnName, Object value) in D:\\Users\\Yaqub.Ahmad\\CS2\\trunk\\code\\MSMainPrj\\Libraries\\TelerikGridHelper.cs:line 292"      string

 

TargetSite         {Void RadGridView1_ValueChanging(System.Object, Telerik.WinControls.UI.ValueChangingEventArgs)}      System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

Kindly help me to update a custom column's value.

Thanks.

 


2 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 24 Aug 2009, 10:41 PM
Hi Yaqub,

I tested your code locally, and it appeared to work properly.

Do you know which object in this call is causing the null reference exception?

e.CellElement.RowInfo.Cells[columnUniqueName].Value = "Some Calculated Value";



0
yaqub
Top achievements
Rank 2
answered on 27 Aug 2009, 03:17 PM

Hi,
Thanks for your reply.

I found the reason, actually after this line of code:
e.CellElement.RowInfo.Cells[columnUniqueName].Value = "Some Calculated Value";

the ValueChanging Events fires and the "CurrentCell" returns NULL .
The line number:3 was generating the error.



1:     private void rgvRoomingList_ValueChanging(object sender, ValueChangingEventArgs e)
2:     {           
3:                GridViewRowInfo rowInfo = rgvRoomingList.Rows[rgvRoomingList.CurrentCell.RowIndex] as GridViewRowInfo;
4:      }

So the solution is that now i am checking that "CurrentCell" is not null.

1:     private void rgvRoomingList_ValueChanging(object sender, ValueChangingEventArgs e)
2:     {           
3:            if (rgvRoomingList.CurrentCell != null)
4:            {
5:                GridViewRowInfo rowInfo = rgvRoomingList.Rows[rgvRoomingList.CurrentCell.RowIndex] as GridViewRowInfo;
6:            }
7:      }


Thanks again.
Tags
GridView
Asked by
yaqub
Top achievements
Rank 2
Answers by
Robert
Top achievements
Rank 1
yaqub
Top achievements
Rank 2
Share this question
or