【问题标题】:WPF DataGrid: How to Determine the Current Row Index?WPF DataGrid:如何确定当前行索引?
【发布时间】:2013-12-08 19:16:49
【问题描述】:

我正在尝试基于 DataGrid 实现一个非常简单的电子表格功能。

  1. 用户点击一个单元格

  2. 用户输入一个值并按回车

  3. 扫描当前行并更新依赖于单击的单元格的任何单元格公式。

这似乎是满足我要求的最佳事件处理程序:

private void my_dataGrid_CurrentCellChanged(object sender, EventArgs e)

问题:如何检测当前行的行索引?

【问题讨论】:

  • 有关导致 CurrentCellChanged 事件被触发的对象的信息通常在 EventArgs e 中可用。尝试设置断点并检查 'e' 为您带来的信息。
  • 没有什么作为 eventargs 是基类而不是派生的:)

标签: c# wpf datagrid datagridcell


【解决方案1】:

试试这个(假设您的网格名称是“my_dataGrid”):

var currentRowIndex = my_dataGrid.Items.IndexOf(my_dataGrid.CurrentItem);

通常,您可以使用my_dataGrid.SelectedIndex,但似乎在CurrentCellChanged 事件中,SelectedIndex 的值总是显示以前 选择的索引。此特定事件似乎在 SelectedIndex 的值实际更改之前触发。

【讨论】:

  • 如果您在ItemsSource 中有重复引用,则会失败。
【解决方案2】:

接受的解决方案将一直有效,直到您在 ItemsSource 中没有引用重复项,否则您将获得对象第一次出现的索引。

BRAHIM Kamel 的解决方案 将一直有效,直到您进行选择,否则(如果您单击两次并取消选择单元格/行)您将没有 SelectedIndex

使用 YourDataGrid.ItemContainerGenerator.ContainerFromItem( _dataItemFromCurentCell ) as DataGridRow,您将始终通过重复获得数据项的最后一次出现。

我会处理 DataGrid.PreviewMouseLeftButtonDown 事件并在处理程序中搜索视觉树到DatagridRow,它有DatagridRow.GetIndex() 方法。 因此您将始终获得正确的行索引

<DataGrid ... PreviewMouseLeftButtonDown="Previe_Mouse_LBtnDown" >
    ...
</DataGrid>

private void Previe_Mouse_LBtnDown(object sender, MouseButtonEventArgs e)
{
    DataGridRow dgr = null;
    var visParent = VisualTreeHelper.GetParent(e.OriginalSource as FrameworkElement);
    while (dgr == null && visParent != null)
    {
        dgr = visParent as DataGridRow;
        visParent = VisualTreeHelper.GetParent(visParent);
    }
    if (dgr == null) { return; }

    var rowIdx=dgr.GetIndex();
}

【讨论】:

    【解决方案3】:

    嗨,你可以做这样的事情来做你的电子表格

     //not recomended  as it always  return the previous index of the selected row 
     void dg1_CurrentCellChanged(object sender, EventArgs e)
        {
    
           int rowIndex = dg1.SelectedIndex;   
         }
    

    但是如果你想要一个更详细的例子,你可以这样做

    namespace WpfApplication2
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Tuple<string,string>> observableCollection = new ObservableCollection<Tuple<string,string>>();  
        public MainWindow()
        {
            InitializeComponent();            
            for (int i = 0; i < 100; i++)
            {
                observableCollection.Add( Tuple.Create("item " + i.ToString(),"=sum (c5+c4)"));
            }
    
            dg1.ItemsSource = observableCollection; 
    
            dg1.CurrentCellChanged += dg1_CurrentCellChanged;
    
        }
    
        void dg1_CurrentCellChanged(object sender, EventArgs e)
        {
            //int rowIndex = dg1.SelectedIndex;   
            Tuple<string, string> tuple = dg1.CurrentItem as Tuple<string, string>; 
            //here as you have your datacontext you can loop through and calculate what you want 
    
        }
    }
    }
    

    希望对你有所帮助

    【讨论】:

      【解决方案4】:

      1.点击Datagrid中的Cell,获取CurrentCell信息

      2.从DatagridCellInfo中,我们可以找到特定单元格的列和行索引

      private void CellClick(object sender,RoutedEventArgs e)
          {
           DataGridCellInfo cell=DataGrid.CurrentCell;
           int columnindex=cell.Column.DisplayIndex;
           int rowIndex=DataGrid.Items.IndexOf(cell.Item);
          }
      

      【讨论】:

      • 在回答一个老问题时,如果您包含一些上下文来解释您的答案如何提供帮助,那么您的答案将对其他 StackOverflow 用户更有用,特别是对于已经有一个已接受答案的问题。请参阅:How do I write a good answer
      • @DavidBuck 不仅是为了回答老问题 :)
      【解决方案5】:
      GRD.Items.Count;
      DataGridRow row = (DataGridRow) GRD.ItemContainerGenerator.ContainerFromIndex(i);     
      DataGridCell TXTGROUPID = GRD.Columns[2].GetCellContent(row).Parent as DataGridCell;               
      string str = ((TextBlock) TXTGROUPID.Content).Text;
      MessageBox.Show(str);
      

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 2011-02-22
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        • 2018-08-17
        • 1970-01-01
        相关资源
        最近更新 更多