【问题标题】:c# wpf datagrid row indexc# wpf datagrid行索引
【发布时间】:2017-04-13 08:09:36
【问题描述】:

我尝试了几种返回当前行索引值的方法,但到目前为止,我的环境中不接受所有建议作为有效代码。我想获得焦点行的索引值或通过我插入的行按钮。这是我的测试代码 - xml

<DataGrid x:Name="dataGrid1"  Loaded="WhenLoaded" AutoGenerateColumns="False" Margin="0,0,478,274" SelectionMode="Single" SelectionChanged="dataGrid1_SelectionChanged" >
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Name="Select" Click="Row_Click" Content="Select" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="node" Binding="{Binding Path=NODE}"   />
    <DataGridTextColumn Header="name" Binding="{Binding Path=NAME}"  />
    <DataGridTextColumn Header="s/n" Binding="{Binding Path=SERIAL_NO}"  />
</DataGrid.Columns>

C#

        myDataSet.Tables.Add(myTable);
        myDataSet.Tables.Add(myTable2);  

        myTable.Columns.Add("NODE", typeof(string));
        myTable.Columns.Add("NAME", typeof(string));
        myTable.Columns.Add("SERIAL_NO", typeof(string));

        myTable.Rows.Add(new string[] { "99", "Pressure", "1234" });

        dataGrid1.ItemsSource = myTable.DefaultView;    

我尝试使用此方法访问索引,但无法识别 RowIndex 和 ColumnIndex -

private void Row_Click(object sender, RoutedEventArgs e)
    {
        int row = dataGrid1.CurrentCell.RowIndex;
        int col = dataGrid1.CurrentCell.ColumnIndex;
    }

【问题讨论】:

    标签: c# wpf datatables


    【解决方案1】:

    使用SelectedIndex属性:

        private void Row_Click(object sender, RoutedEventArgs e)
        {
            var selectedRowIndex = dataGrid1.SelectedIndex
    
        }
    

    或者,您可以从当前单元格获取行索引(虽然有点多余):

    var row = (DataGridRow)dataGrid1.ItemContainerGenerator
              .ContainerFromItem(dataGrid1.CurrentCell.Item);
    Console.WriteLine(row.GetIndex());
    

    wpf DataGridCell 上没有列索引,但您可能会得到 DisplayIndex(注意当列重新排列时它可能会改变)

    Console.WriteLine(dataGrid1.CurrentCell.Column.DisplayIndex);
    

    如果您想知道您正在访问的数据中的哪一列/属性,您需要根据 DisplayIndex 以外的其他内容找到它(例如在 CurrentCell.Column.Header 上)

    如果您想在列中不放置按钮的情况下获取索引,可以使用例如DataGrid.MouseUpDataGrid.MouseLeftButtonUp 事件。

    你可以找到更深入的解释here

    【讨论】:

    • 谢谢,知道怎么做就简单了。 SelectedIndex 正是我想要的。
    【解决方案2】:
    private void Row_Click(object sender, RoutedEventArgs e)
    {
          DataRowView dataRow = (DataRowView)dataGrid1.SelectedItem;          
          string cellValue_Method1 = dataRow.Row.ItemArray[3].ToString();
          string cellValue_Method2 = dataRow[0].ToString();
          int index = dataGrid1.CurrentCell.Column.DisplayIndex;
          MessageBox.Show("index "+index + " cell value frist from method: "+cellValue_Method1 + " cell value from second method: "+cellValue_Method2);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2011-05-05
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多