如何访问DataGrid列值

时间:2016-01-13 13:03:56

标签: c# silverlight datagrid

我有一个自动生成的DataGrid,我想要检索ID值。每行都有一个HyperLink button,我想将该行ID的值发送给new page via query stringProductGrid的顺序如下 (HyperLinkButton,ID,Name,Details)

这就是我绑定网格的方式:

public void webService_GetProductsCompleted(object sender, ServiceReference1.GetProductsCompletedEventArgs e)
{
  ProductGrid.ItemsSource = e.Result;
  ProductGrid.Visibility = Visibility.Visible;
}

这是HyperLink按钮点击事件,我尝试检索单元格值但是为空(注释)

private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
  //string var = ProductGrid.Columns[1].GetCellContent(ProductGrid.SelectedItem).ToString();
  this.NavigationService.Navigate(new Uri("/Home?", UriKind.Relative));
}

有人可以帮我找出如何在查询字符串中发送单元格值吗?

1 个答案:

答案 0 :(得分:0)

您可以使用RowCommand事件或尝试这种方式。

private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
LinkButton lnkbtn= sender as LinkButton;
//getting particular row linkbutton
GridViewRow grow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
string prodid = ProductGrid.DataKeys[gvrow.RowIndex].Value.ToString();
string prodname = grow.Cells[0].Text;
}