【问题标题】:How to avoid exceptions in utilizing a value of datagridrow when row is not selected未选择行时如何避免在使用 datagridrow 值时出现异常
【发布时间】:2016-06-03 21:45:54
【问题描述】:

我有一个 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;

【问题讨论】:

    标签: c# wpf exception select datagrid


    【解决方案1】:

    在使用所选项目之前验证它是否为空

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

    【讨论】:

    • 哦,它工作正常。请理解我的专业是与软件行业无关的生物化学。经过这么长时间的孤独挣扎,我似乎变得过于依赖stackoverflow。非常感谢您让我领先一步。非常感谢!
    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多