Click here to Skip to main content
15,179,830 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 20-Dec-16 0:24am
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[^]
   
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;
}

}
Afzaal Ahmad Zeeshan 20-Dec-16 5:42am
   
That does not check for any null values. That is why, it will not work for what I am saying. You need something like,

if (drv != null) {
// code.
}

Something like this.
krish.krish 20-Dec-16 5:50am
   
drv value is null
Afzaal Ahmad Zeeshan 20-Dec-16 5:59am
   
Then set it to a new object. :-)

Such as, new DataRowView etc.
krish.krish 20-Dec-16 5:42am
   
please help me i want datagrid selected row data in another form with filling

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