21

DataGrid に基づいた非常に単純なスプレッドシート機能を実装しようとしています。

  1. ユーザーがセルをクリックする

  2. ユーザーが値を入力して Return キーを押す

  3. 現在の行がスキャンされ、クリックされたセルに依存するセル式が更新されます。

これは、私の要件に最適なイベント ハンドラーのようです。

private void my_dataGrid_CurrentCellChanged(object sender, EventArgs e)

質問: 現在の行の行インデックスを検出するにはどうすればよいですか?

4

5 に答える 5

43

これを試してください(グリッドの名前が「my_dataGrid」であると仮定します):

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

通常は を使用できますがmy_dataGrid.SelectedIndexCurrentCellChangedイベントでは SelectedIndex の値が常に以前に選択されたインデックスを表示するようです。この特定のイベントは、SelectedIndex の値が実際に変更される前に発生するようです。

于 2013-11-24T01:23:28.980 に答える
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 

    }
}
}

この助けを願っています

于 2013-11-24T01:28:46.137 に答える
-3
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);
于 2015-09-07T06:26:11.763 に答える