WPF中的datagrid点击一个单元格,怎么获取单元格里面的值
2个回答
展开全部
Text4.Text = DataGrid1.Text
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给段我写的Demo哈,有不理解的地方欢迎提问
前台代码:
<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<DataGrid Name="dg" AutoGenerateColumns="True" RowHeaderWidth="0" SelectedCellsChanged="dg_SelectedCellsChanged" SelectionUnit="Cell"></DataGrid>
<TextBlock Grid.Row="1" Name="txt"/>
</Grid>
后台代码:
public partial class BaiduKnows : Window
{
public BaiduKnows()
{
InitializeComponent();
List<People> peoplelist = new List<People>();
peoplelist.Add(new People() { age = 10, name = "linda" });
peoplelist.Add(new People() { age = 11, name = "tom" });
peoplelist.Add(new People() { age = 12, name = "peter" });
peoplelist.Add(new People() { age = 13, name = "mary" });
dg.ItemsSource = peoplelist;
}
class People
{
public int age { get; set; }
public string name { get; set; }
}
private void dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
DataGrid dg = sender as DataGrid;
People item = dg.SelectedCells[0].Item as People;
string colhead = dg.SelectedCells[0].Column.Header.ToString();
switch (colhead)
{
case "age" : txt.Text = item.age.ToString();
break;
case "name": txt.Text = item.name.ToString();
break;
}
}
}
可能有更好的做法,我这个只是提供一种思路哈!~
前台代码:
<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<DataGrid Name="dg" AutoGenerateColumns="True" RowHeaderWidth="0" SelectedCellsChanged="dg_SelectedCellsChanged" SelectionUnit="Cell"></DataGrid>
<TextBlock Grid.Row="1" Name="txt"/>
</Grid>
后台代码:
public partial class BaiduKnows : Window
{
public BaiduKnows()
{
InitializeComponent();
List<People> peoplelist = new List<People>();
peoplelist.Add(new People() { age = 10, name = "linda" });
peoplelist.Add(new People() { age = 11, name = "tom" });
peoplelist.Add(new People() { age = 12, name = "peter" });
peoplelist.Add(new People() { age = 13, name = "mary" });
dg.ItemsSource = peoplelist;
}
class People
{
public int age { get; set; }
public string name { get; set; }
}
private void dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
DataGrid dg = sender as DataGrid;
People item = dg.SelectedCells[0].Item as People;
string colhead = dg.SelectedCells[0].Column.Header.ToString();
switch (colhead)
{
case "age" : txt.Text = item.age.ToString();
break;
case "name": txt.Text = item.name.ToString();
break;
}
}
}
可能有更好的做法,我这个只是提供一种思路哈!~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询