【问题标题】:Gathering data from datagrid in a specified column and row从数据网格中收集指定列和行中的数据
【发布时间】:2013-03-06 22:27:19
【问题描述】:

在我的 WPF 应用程序中,我正在数据网格中浏览数据库。我的代码的重点是将数据网格中选定的单元格内容存储到一些值列表中以供进一步操作。我的代码适用于 12 岁以下的选定项目,但对于更多项目,它会抛出一个 NullRefferenceException,上面写着“

对象引用未设置为对象的实例”。

感谢您的帮助。

代码:

List<string> graphValue = new List<string>(dataGrid1.SelectedItems.Count); //create list 
IList someList = new ArrayList(dataGrid1.SelectedItems); //define Ilist
DataGridColumn dataGridCol = dataGrid1.Columns[listBox1.SelectedIndex]; 
//select column whom i wana collect data

if (dataGrid1.SelectedItems != null)    //when selection applied..
{
   for (int i = 0; i < dataGrid1.SelectedItems.Count; i++) //go row by row in selected column above
   {
      try
      {
         id = ((TextBlock)dataGridCol.GetCellContent(someList[i])).Text.ToString(); //save cell content to string
         graphValue.Add(id); //add value to Ilist
      }

      catch (Exception ex)
      { 
         System.Windows.MessageBox.Show(ex.Message, "error"); }
      }
    }
  }

【问题讨论】:

  • 不要在 WPF 的代码中操作 UI 元素。学习 MVVM。创建一个适当的 ViewModel 来保存您的数据。视觉树太复杂了,你的代码不断地将视觉元素转换成你期望的样子,真的很容易出错。它通过强制 UI 元素实际位于代码期望的位置来降低可伸缩性。
  • 哪一行抛出异常?有东西返回null...
  • 在这一行抛出异常:id = ((TextBlock)dataGridCol.GetCellContent(someList[i])).Text.ToString(); 但输入值都不是null 我只是选择更多数据输入(更多行)

标签: c# wpf datagrid


【解决方案1】:

这个简单的代码可能会对你有所帮助这里首先我们将遍历 Data-grid 的每条记录,然后通过提供 Row 和 Column as.. 选择特定的单元格/列数据循环。

for(int row =0; row < dg_CountInventory.Rows.Count; row ++)
{
TextBlock b = dg_CountInventory.Columns[1].GetCellContent(dg_CountInventory.Items[row ]) as TextBlock;
}

【讨论】:

    【解决方案2】:

    当你用

    定义你的someList
    IList someList = new ArrayList(dataGrid1.SelectedItems);
    

    您实际上创建了一个ArrayList,所有值都是null。之后,您尝试使用它归档GetCellContent() 方法。

    dataGridCol.GetCellContent(someList[i])).Text
    

    someList[i] 可能是null,这就是你得到NullRefferenceException 的原因。在使用它之前添加这个数组的一些值。

    【讨论】:

    • 是的,我也是这么想的,但是 someList 中的对象System.Data.DataRowView 都不是空的,所以我正在尝试使用相关数据。问题可能出在TextBlock 或其他地方吗?
    • 编辑:我实际上创建了一个Arraylist,其中包含来自DataGrid 属性datagrid1.SelectedItems 的选定行。所有选定的行都包含数据、每一列、每个单元格。问题将出在一些我看不到的基础知识上,因为我刚从 C# 开始,我正在自学,很抱歉给您带来不便。
    【解决方案3】:

    我在谷歌上搜索,找到了适合我的解决方案。我会在这里给其他人。如果有人能解释我在前面的代码中做错了什么,我将不胜感激。

    此代码适用于我:

    if (dataGrid1.SelectedItems.Count > 0)    //when selection applied..
      {
       for (int i = 0; i < dataGrid1.SelectedItems.Count; i++) //go row by row in selected column above
          {
           try
             {
              System.Data.DataRowView selectedFile = (System.Data.DataRowView)dataGrid1.SelectedItems[i];
              string str = Convert.ToString(selectedFile.Row.ItemArray[listBox1.SelectedIndex]);
              graphValue.Add(str);
             }
           catch (Exception ex)
             { System.Windows.MessageBox.Show(ex.Message, "error"); }
          }
    

    现在我可以在DataGrid 中选择超过 10 行。 参考:http://www.codeproject.com/Questions/119505/Get-Selected-items-in-a-WPF-datagrid 现在我真的很高兴,谢谢大家的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2021-03-27
      相关资源
      最近更新 更多