Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Error
 

DataGridView Error

When I execute the following code:

myleaserow.Cells[5].Value = ((decimal)(myleaserow.Cells[8].Value) * (decimal)(myequiprow.Cells[1].Value)) / 1000;

I get an Object referance not set to an instance of an object error.

How do I fix this?

The purpose of doing this is so that I may edit the cell in the datagridview and then insert it into the database. What is strange is that after updating, the error message pops up, but if I choose to continue the new value is displayed. However, the changes are not forwarded to the database.

Is there another way of accomplishing the same task?

Thank You,

Protecyon
MigrationUser 1  Thursday, April 21, 2005 12:37 AM
First you take the cell value in a variable then try it i hope it will work
regards
prathvi
MigrationUser 1  Thursday, April 21, 2005 4:45 AM
Thank you for your help, the error turned out to be completely unrelated. I was getting the error, because I was accessing a row that did not exist. 

The code that was giving me the error turned out to be: 

for (int i = 0; i < this.eDataGridView.Rows.Count; i++) 




It should have been: 

for (int i = 0; i < this.eDataGridView.Rows.Count-1; i++) 




A classic mistake. 

However, I have another question about the following code: 

for (int i = 0; i < this.eDataGridView.Rows.Count - 1; i++) 

lrow = lDataGridView.Rows[i]; 
erow = eDataGridView.Rows[i]; 

if (lrow.Cells["1"].Value.ToString() != "" && erow.Cells["2"].Value.ToString() != "" && lrow.Cells["3"].Value.ToString()!="") 

lrow.Cells["4"].Value = (((decimal)lrow.Cells["1"].Value * (decimal)erow.Cells["2"].Value)/1000)+(decimal)lrow.Cells["3"].Value; 



The purpose of the code is to fill in cell 4 within row l after it has sufficient information. The information however does not become available right away as the user first types in information in cell 1 and 3 in lrow, and then types in information for cell 2 in erow. Only then can cell 4 in lrow be filled in. 

However, I am looking for a better way of accomplishing this, since this method is run right before the update method, and consumes a lot of resources since it transverses all rows even those that might have already been filled in. 

Would the best way of accomplishing this task be through the CellChangedEvent, in which I would check whether the user has changed one of the cells that I am interested in, and then to go and check whether the rest of the cells that I am interested in have also changed. Then I could fill in cell 4 in lrow if I have sufficient information. Of course if I do not have sufficient information I can prompt the user to enter it at this point. Or is there a better way?
MigrationUser 1  Monday, April 25, 2005 9:13 PM

You can use google to search for other answers

Custom Search

More Threads

• UserPreferenceChangedEventHandler
• SelectedIndexChanged event not handling combo box changes properly
• linkLabel column in datagrid
• DataGridView - Stop User Selecting Cells
• Inserting Values to SQL from Textbox
• Problem binding a dataset to controls
• Hotel reservation schedule - dataGridView (C#)
• How do I Fill a DataSet (using VB.NET) without freezing up the GUI?
• Checkedlistbox and Databinding
• combobox in datagridview question