如何从WPF中的数据网格中选择数据 [英] how to select data from datagrid in WPF
本文介绍了如何从WPF中的数据网格中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,
我想从WPF中的DataGrid
到TextBox
接收数据(数据网格中的一行). DataGrid
已从数据库(SQL Server)中的表中获取值.
单击特定行时,我想在DataGrid
至TextBox
中选择一行
我写的代码如下:
Sir,
I want to take data (a row in the datagrid) from DataGrid
in WPF to TextBox
. The DataGrid
has taken the values from a table in the database (SQL Server).
I want to select a row in the DataGrid
to TextBox
when click on the particular row
I wrote the code as follows:
private void branchdg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtbranchcode.Text = branchdg.SelectedItem.ToString();
}
但它会将输出作为System.Data.DataRowView
提供给TextBox
.
这可能很简单,但是请帮帮我
提前致谢...
but it gives the output as System.Data.DataRowView
to the TextBox
.
It may be very simple, but pls help me
Advance thanks...
推荐答案
如果已将datagrid设置为允许选择,则该事件应具有选择更改事件.这将告诉您选择了哪一行.
The datagrid should have a selection changed event, if you''ve set it to allow selection. This will tell you what row is selected.
该项目是一个DataRowView,因此您将获得该内容.相反,您需要使用SelectedItem并从其中的列中获取数据.我通常建议您阅读一本有关C#的书,这样您将了解该结果的含义,并考虑阅读MSDN以了解如何使用您以前从未使用过的对象和方法.
The item IS a DataRowView, so that''s what you get. You need instead to take the SelectedItem and grab the data from the columns inside it. I suggest in general that you should read a book on C#, so you''d understand what this result means, and consider reading MSDN to learn how to use objects and methods you''ve not used before.
这篇关于如何从WPF中的数据网格中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文