Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
About wpf Datagrid MouseUp event, I googled but can not get What's this error? My snippets as follow:

XML
<DataGrid x:Name="gridTable1">
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseUp" Handler="dataGridRow_MouseUp"/>
        </Style>
    </DataGrid.ItemContainerStyle>
</DataGrid>



Code Behind:
C#
private void dataGridRow_MouseUp(object sender, MouseButtonEventArgs e)
   {
       DataGridRow dgr = sender as DataGridRow;
       DataRowView drv = dgr.DataContext as DataRowView; //This error: Object reference not set to an instance of an object

       string requestId = drv.Row["RequestID"].ToString();

       DataTable dt = new DataTable();
       dt = db.MySelect("select RequestID from Requests where RequestID='" + requestId + "'");
       gridTable2.ItemsSource = dt.DefaultView;
   }


This error occured: Object reference not set to an instance of an object.
Is there any code sample?
Posted
Updated 20-Feb-14 3:29am
v2

Clearly, the sender is not a DataGridRow - so dgr gets a null.
At a guess, it's a DataGrid, not a row - but look in the debugger and it should tell you.
 
Share this answer
 
The 100000000th answer to the same question... (Not your fault of course)

So:
Use your Debugger! Set a breakpoint, have a look what's null. This is so basic and essential, there is no other way arround this (knowledge of basic debugging).
If I have to guess, I'd say your DataContext is not a DataRowView (have a look how the as operator behaves - it returns a null reference if the object is not convertible) or no DataContext is assigned (unlikely)

I hope you can spot it quickly, good luck with your Project!
 
Share this answer
 
Comments
M.H. Shojaei 20-Feb-14 9:55am    
Thank j. drv is null

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