2017-01-03 5 views
0

現在、データが格納されているWPFデータグリッドがあります。VB.netデータグリッドの列と行からセルデータを返す方法

データグリッドが全行選択モードのときに、ユーザーがクリックしたセルのセル値を返したいとします。

現時点で私はユーザーが選択したセルの行と列を返すことができますが、実際のセル値を返す方法はわかりません。

ここは私のVBコードです。 SelectedCellsChangedEventが発射された場合

Private Sub WaterfallDataGrid_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles WaterfallDataGrid.SelectedCellsChanged 

     'return the column of the cell of the datagrid 
     textBox1.Text = WaterfallDataGrid.CurrentCell.Column.DisplayIndex 

     'return the row of the cell of the datagrid 
     textBox2.Text = WaterfallDataGrid.Items.IndexOf(WaterfallDataGrid.CurrentItem) 

     'how do i return the value of the cell itself? 
     'textBox3.Text = ?? 

    End Sub 

答えて

1

は、あなたがSelectedCellsChangedEventArgsで動作するように取得します。このオブジェクトには、AddedCellsというプロパティがあります。ユーザーがセルをクリックすると、行全体が選択されるので、行全体はAddedCellに含まれます。あなたはすでにあなたのコードから列番号を知っているので、これは動作するはずです:

Private Sub WaterfallDataGrid_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles WaterfallDataGrid.SelectedCellsChanged 

    textBox3.Text = e.AddedCells(WaterfallDataGrid.CurrentCell.Column.DisplayIndex).Item(WaterfallDataGrid.CurrentCell.Column.DisplayIndex) 

End Sub 

出典:

https://msdn.microsoft.com/en-us/library/system.windows.controls.selectedcellschangedeventargs(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.items(v=vs.110).aspx

編集: セル内のイベントのみが発生します新しい行が選択されます。同じ行のセルを選択すると、何も起こりません。常に現在のセルを取得するには、DataGridにCurrentCellChangedイベントを登録する必要があります。

は、あなたのXAMLにCurrentCellChangedイベントを追加します。

<DataGrid x:Name="WaterfallDataGrid" CurrentCellChanged="WaterfallDataGrid_CurrentCellChanged" /> 

そして、これはあなたの分離コードに:

Private Sub WaterfallDataGrid_CurrentCellChanged(sender As Object, e As EventArgs) 
    textBox3.Text = WaterfallDataGrid.CurrentCell.Item(WaterfallDataGrid.CurrentCell.Column.DisplayIndex) 
End Sub 
+0

コードをコンパイルしますが、原因 "型に型「DataRowView」からの変換にランタイムエラーがスローされます文字列 '。私はそのステートメントの後ろに.ToStringを追加しました。ランタイムエラーは発生しませんが、次のような結果が返されます。 "System.Data.DataRowView"何か案は? – Tofulover

+0

データグリッドにはどのようなデータがありますか?どのようにデータを挿入しますか? – Pie

+0

DataGridにはこれまでと同様のコードが作成されています。dt2.Rows.Add(最終的な劣化ケーブルの容量(A))、 GlobalVariables.DeratedCCC(1)、 GlobalVariables.DeratedCCC(2)、 GlobalVariables.DeratedCCC (3)、 ) CableValuesDataGrid.ItemsSource = dt2.DefaultView – Tofulover

関連する問題