WPF中DataGrid使用时,需要将其SelectedItem转换成DataRowView进行操作

然而SelectedItem SelectedItems DataGrid的SelectionUnitSelectionMode两个属性的取值不同时有变化

一:当DataGrid.SelectionUnit == DataGridSelectionUnit.FullRow时,获取选中一行与多行的方法:

1选中多行

int count = DataGrid.SelectedItems.Count;

DataRowView[] drv = new DataRowView[count];

for (int i = 0; i < count; i++)

{

drv[i] = DataGrid.SelectedItems[i] as DataRowView;

}

return drv;

2选中一行

DataGrid.SelectedItem as DataRowView

二:但是当DataGrid.SelectionUnit 的属性是Cell或者CellOrRowHeader时,并且SelectionMode的值为 Extented时,这样处理就不太好。因为如果选中的是 cell 则SelectedItem的值为null。所以可以通过Cell来统一处理,无论SelectionUnit 的值是什么,总有选中的单元格,通过单元格确定该行。

private DataRowView GetSelectedRow()

{

/*优化

* 无论 DataGrid的SelectionUnit跟SelectionMode两个属性取任何值

* 都存在选中的单元格

* 可以根据选中的单元格做统一处理,获取选中的行

*  GetSelectedRows()方法获取选中多行原理相同

*/

if (DataGrid != null && DataGrid.SelectedCells.Count != 0)

{

//只选中一个单元格时:返回单元格所在行

//选中多个时:返回第一个单元格所在行

return DataGrid.SelectedCells[0].Item as DataRowView;

}

return null;

}

/// <summary>

/// 私有方法 获取选中的多行

/// </summary>

/// <returns></returns>

private DataRowView[] GetSelectedRows()

{

//当选中有多个单元格时,获取选中单元格所在行的数组

//排除数组中相同的行

if (DataGrid!=null&&DataGrid.SelectedCells.Count > 0)

{

DataRowView[] dv = new DataRowView[DataGrid.SelectedCells.Count];

for (int i = 0; i < DataGrid.SelectedCells.Count; i++)

{

dv[i] = DataGrid.SelectedCells[i].Item as DataRowView;

}

//因为选中的单元格可能在同一行的,需要排除重复的行

return dv.Distinct().ToArray();

}

return null;

}

WPF DataGrid 获取选中 一行 或者 多行的更多相关文章

  1. c# WPF DataGrid 获取选中单元格信息

    private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...

  2. 【转】WPF DataGrid 获取选中的当前行某列值

    方法一:DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; string result = mySelectedE ...

  3. WPF DataGrid 获取选中的当前行某列值

    方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...

  4. WPF DataGrid获取选择行的数据

    在WPF中,单击DataGrid,如何获取当前点击的行? 比如在MouseDoubleClick事件中,事实上获取的选中行是一个DataRowview,你可以通过以下的方法来获取选中行的数据,需要引用 ...

  5. WPF TextSelection获取选中部分内容

    一.简单实例 //TextSelect继承自TextRange TextSelection selection = richTextBox.Selection; //1.获取选中内容 string r ...

  6. wpf ComboBox 获取选中项的文本内容

    一:根据数据源类型获取选中项 类: public class Region { public int REGION_ID { get; set; } public string REGION_CODE ...

  7. WPF datagrid 获取行或单格为NULL 问题

    datagrid  属性 EnableRowVirtualization 设置为 false 解决...不要问我为什么. 害死我了

  8. jquery easyui datagrid 获取选中多行

    var rows = $('#dataTable').datagri('getSelections');

  9. WPF DataGrid 增加"更新"模板列,根据行Row的选择而显示"更新"按钮

    SelectionMode="Single" <DataGridTemplateColumn Header=""> <DataGridTemp ...

随机推荐

  1. Leetcode 332.重新安排行程

    重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...

  2. ubuntu14.04 software-center can not open

    sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install --reinstall software-center

  3. zoj 2830 Champion of the Swordsmanship

    Champion of the Swordsmanship Time Limit: 2 Seconds      Memory Limit: 65536 KB In Zhejiang Universi ...

  4. iOS学习笔记13-网络(二)NSURLSession

    在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...

  5. 【bzoj4408】[Fjoi 2016]神秘数 主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 = 4+1+1 ...

  6. P3147 [USACO16OPEN]262144 (贪心)

    题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...

  7. bzoj3196 二逼平衡树 树套树(线段树套Treap)

    Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4697  Solved: 1798[Submit][Status][D ...

  8. jmeter录制接口以及并发测试

    http://jingyan.baidu.com/article/15622f2475601dfdfdbea548.html

  9. Python爬虫--beautifulsoup 4 用法

    Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构, 每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSo ...

  10. 每日记录 2016-4-29 HTML5本地存储

    HTML5本地存储 一.HTML5 localStorage 在HTML5中,本地存储是一个window的属性,包括localStorage和 sessionStorage,从名字应该可以很清楚的辨认 ...