WPF DataGrid 没有行的概念吗?怎么能拿到选中行的索引? DataGrid.SelectedIndex 返回的值是-1,要怎么拿?

作者&投稿:晁阳 (若有异议请与网页底部的电邮联系)
VS中 DataGridView获取选中行的索引值??~

Private Sub DataGridView1_CurrentCellChanged(By Val sender As Object, By Val e As System.EventArgs) Handles DataGridView1.CurrentCellChanged

//获取选中行第一列的值,也就是第0列的值
Dim result1 As String = DataGridView1.Item(0, DataGridView1.CurrentCell.RowIndex).Value.ToString.Trim

//获取选中行第二列的值,也就是第1列的值
Dim result1 As String = DataGridView1.Item(1, DataGridView1.CurrentCell.RowIndex).Value.ToString.Trim

'......有几列就写几列,如果感觉这样写代码比较累赘,你可以放到for循环里面,把列数用一个变量i代替旧可以了

End sub

datagridview.CurrentCell.RowIndex;是当前活动的单元格的行的索引
datagridview.SelectedRows 是选中行的集合
datagridview.SelectedColumns 是选中列的集合
datagridview.SelectedCells 是选中单元格的集合
DataGridView1.CurrentRow.Index 获得包含当前单元格的行的索引

我帮你写了一段代码,你试试

// dg = dataGrid

var index = dg.SelectedIndex;

if (index == -1) // 判断是否选中了某一行
{
    if (dg.SelectedCells.Count > 0)
    {
        // 获取综合信息,包括行、列索引
        // DataGridRow 对象
        // DataGridCell 数据模板中的首元素
        // 获取绑定到 DataGridRow 上的数据对象
        var infos = dg.SelectedCells.Select(s => new
        {
            ColumnIndex = s.Column.DisplayIndex,
            RowIndex = dg.Items.IndexOf(s.Item),
            DataItem = s.Item, // 该行所绑定的数据对象
            // 根据 s.Item 获取 DataGridRow
            DataGridRow = dg.ItemContainerGenerator.ContainerFromItem(s.Item),
            // 根据 s.Item 获取单元格模板的首个元素
            VisulRoot = s.Column.GetCellContent(s.Item),
        });

        foreach (var info in infos)
        {
            Console.WriteLine("{0}: ({1},{2})",
                info.VisulRoot, info.RowIndex, info.ColumnIndex);
        }
    }

    // 获取 DataGridRow 绑定的数据对象
    var items = dg.SelectedCells.Select(s => s.Item).Distinct();
}
else
{
    /* SelectedIndex != -1 */
}


我的测试结果是这样的:
SelectedIndex——在SelectedCellsChanged事件中,如果SelectionUnit是FullRow,则这个值返回的就是当前击中的行号,如果SelectionUnit是Cell或CellOrRowHeader则点击时总是返回-1。要是在CurrentCellChanged事件中取得这个值,如果SelectionUnit是FullRow,则每次取得的都是上一次选中行的行索引值,第一次点击一行则返回-1(CurrentCellChanged在SelectedCellsChanged前触发),如果SelectionUnit是Cell或CellOrRowHeader则点击时总是返回-1。参照前面答友,当SelectIndex取值不对时,可以用DataGrid1.Items.IndexOf(DataGrid1.CurrentItem)取得击中的行号。
感觉这应该是是个坑吧,或者理解的不到位。

肯定是有行的,是不是操作不对,贴一下代码看一下,或者你试试SelectedItem,或者说明一下具体需求,希望对你有帮助


舞钢市17889963064: WPF DataGrid 没有行的概念吗?怎么能拿到选中行的索引? DataGrid.SelectedIndex 返回的值是 - 1,要怎么拿? -
桑雪生长: DataGrid 有3中选择模式 (SelectionUnit):单元格 Cell / 行 FullRow / 单元格或行标题 CellOrRowHeader.只有当 SelectionUnit 为 FullRow 和 CellOrRowHeader (且要选中行标题) 才能返回 DataGrid.SelectedIndex

舞钢市17889963064: wpf: datagrid 无数据还显示空行,怎么处理 -
桑雪生长: 加断点测试过dataGrid的数据集合有数值了没?或者有对DataGrid的模板做过什么修改没?XMAL代码和后台赋值代码贴出来看看啊~

舞钢市17889963064: wpf 如何在datagrid后面加入一行文本 -
桑雪生长: 将子datagrid放入一个容器中,然后这个容器在子datagrid的下方增加一个Textblock用于显示文本

舞钢市17889963064: WPF使用自动生成时怎么获得DataGrid的行数 -
桑雪生长: dataGrid1.SelectedItem 是你dataGrid选中的行所代表的数据实体对象 可以用以下方式获取和设置该对象的属性 (dataGrid1.SelectedItem as 实体对象).属性 不太明白你指的textbox是什么.默认表格中的数据都是在前台可以修改的.如果前台修改想改...

舞钢市17889963064: 为什么WPF窗体中无法添加DataGrid -
桑雪生长: 看一下最外层<Window>有没有 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 没有就加上

舞钢市17889963064: wpf中怎么为datagrid绑定一个list 我的代码如下,为什么显示出来的全是空行?... 似乎数据个数是对的… -
桑雪生长: 以前我刚玩wpf的时候也遇到过这种情况的,后来才发现我的class没有设置成public.你看看你是不是也是这种情况.

舞钢市17889963064: 请问WPF DataGrid SelectionChanged事件中怎么获取行啊?dataGridRow.Background好像只有获取行后才设置
桑雪生长: datagridrow.background不需要获取行后才能设置的,比如你在trigger设置. &lt;Style TargetType="DataGridRow"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="Background" Value...

舞钢市17889963064: 为什么vs2010中WPF的控件DataGrid是灰化的,无法使用?? -
桑雪生长: DataGrid默认是可以使用的,你必须在xaml或是在后台给它的ItemsSource赋上一个集合,这样才能在前台显示数据.

舞钢市17889963064: WPF的DataGrid怎么实现多行表头 -
桑雪生长: <Style x:Key="CityNumStyle" TargetType="DataGridColumnHeader"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid x:Name="Root"> <!--<Rectangle x:Name="BackgroundGradient" Fill="#eee" Stretch="...

舞钢市17889963064: WPF DataGrid CanUserAddRows问题 -
桑雪生长: 你绑定的IList有没有实现INotifyPropertyChanged呢,否则数据源有更新,通知不到前台的哟.还有就是DataGrid 的列里绑定属性时需要设置Mode=TwoWay.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网