Click here to Skip to main content
15,895,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
double click datagrid row to open edit form in wpf c#


i want to open selected row grid details to another form with filling.

Company editcompany = new Company();

DataRowView drv = dgvCommon.SelectedItem as DataRowView;

editcompany.cmpID = Convert.ToInt32(drv["CompanyId"].ToString());

NavigationService navService = NavigationService.GetNavigationService(this);

navService.Navigate(editcompany);

What I have tried:

i got error in this line like
Object reference not set to an instance of an object.


editcompany.cmpID = Convert.ToInt32(drv["CompanyId"].ToString());
Posted
Updated 19-Dec-16 23:24pm
v2

1 solution

Error message is pretty much neat and tells you clearly what is wrong. Your object is null, use this,
C#
editcompany.cmpID = Convert.ToInt32((drv["CompanyId"] ?? String.Empty).ToString());

You can also use a simple if...else block to check for null values prior.

What Is A Null Error in Code Execution[^]
c# - Checking for null before ToString() - Stack Overflow[^]
C# approach to prevent null references in strings - Code Review Stack Exchange[^]
 
Share this answer
 
Comments
krish.krish 20-Dec-16 5:31am    
again its showing same error on that line
Afzaal Ahmad Zeeshan 20-Dec-16 5:33am    
Then make sure your entire object is not null — wrap the code inside an if...else, and debug the code to see where an object is null. Then set those object(s) to an instance.
krish.krish 20-Dec-16 5:36am    
i already have switch case statement.
Afzaal Ahmad Zeeshan 20-Dec-16 5:39am    
How do you implement a switch statement? Does it check for any null values?
krish.krish 20-Dec-16 5:40am    
see my code

switch (formname)
{
case "Company Master":
{
Company editcompany = new Company();


DataRowView drv = dgvCommon.SelectedItem as DataRowView;


editcompany.cmpID = Convert.ToInt32((drv["CompanyId"] ?? String.Empty).ToString());



editcompany.operation = Operation;


NavigationService navService = NavigationService.GetNavigationService(this);


navService.Navigate(editcompany);

break;
}
case "Customer Master":
{
Customer editcustomer = new Customer();

DataRowView drv = dgvCommon.SelectedItem as DataRowView;


editcompany.cusid= Convert.ToInt32((drv["cusid"] ?? String.Empty).ToString());




editcustomer.operation = Operation;


NavigationService navService = NavigationService.GetNavigationService(this);


navService.Navigate(editcustomer);
break;
}

}

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