DataGrid MouseUp时,对象引用未设置...错误 [英] Object reference not set... error, when DataGrid MouseUp

查看:76
本文介绍了DataGrid MouseUp时,对象引用未设置...错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于wpf Datagrid MouseUp事件,我用Google搜索但无法获取这是什么错误?我的片段如下:



About wpf Datagrid MouseUp event, I googled but can not get What's this error? My snippets as follow:

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







Code Behind:




Code Behind:

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?

推荐答案

显然,发件人不是DataGridRow - 所以dgr得到一个null。

猜测,它是一个DataGrid,而不是一行 - 但是在调试器中查看它应该告诉你。
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.


对它的第100000000个回答问题...(当然不是你的错)



所以:

使用你的调试器!设置一个断点,看看什么是null。这是基本和必要的,没有其他方法(基本调试的知识)。

如果我不得不猜测,我会说你的DataContext不是DataRowView(看看怎么样 as 运算符的行为 - 如果对象不可转换则返回空引用)或者没有分配DataContext(不太可能)



我希望你能快速发现它,祝你的项目好运!
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!


这篇关于DataGrid MouseUp时,对象引用未设置...错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆