繁体   English   中英

未选择行时如何避免利用datagridrow值的异常

[英]How to avoid exceptions in utilizing a value of datagridrow when row is not selected

我有一个Datagrid,并通过以下代码获得了所选Datagridrow的值。 如果我在选择某个Datagridrow之后运行代码,则可以正常工作。 但是,在客户端(我的软件的用户)上,总是有很多特殊情况,例如单击按钮而不选择Datagridrow。
设置datagrid的selectedindex = 0或1不能解决此问题。

我如何避免这些ArgumentOutOfRangeException,NullreferenceException? 您的卓越将受到高度赞赏。 非常感谢 !

private void buttonFrequentsentencesdeletion_Click(object sender, RoutedEventArgs e)
    {
        Button bu = sender as Button;

        if (sender != null)
        {
            object item = dataGrid_Frequentsentences.SelectedItem;
            string FSdatagridID = (dataGrid_Frequentsentences.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;

在使用所选项目之前,请验证其是否为空

private void buttonFrequentsentencesdeletion_Click(object sender, RoutedEventArgs e)
{ 
     if (dataGrid_Frequentsentences.SelectedItem != null)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM