datagrid 如何获取列数据

yangchun20080422 2012-07-25 09:08:12
急急急!!


string sLon;
sLon = dataGrid2.Columns[4].ToString();


获取不到列的数据???
...全文
70 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jv9 2012-07-25
  • 打赏
  • 举报
回复
没有直接的API获取Column数据,可以通过以下代码:


private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;

int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;

DataGridColumn column = dataGrid.Columns[0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));

if (result != null)
{
DataGridCell cell = (DataGridCell)result;

cell.Foreground = new SolidColorBrush(Colors.Blue);

TextBlock block = fe as TextBlock;
if (block != null)
{
string cellText = block.Text;
MessageBox.Show(cellText);
}
}
}
}
你的选择H 2012-07-25
  • 打赏
  • 举报
回复
应该是某个单元格吧
string sLon=(dataGrid2.Columns[4].GetCellContent(e.Row).FindName("txtbox") as TextBox).Text;

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧