【问题标题】:Get string value of the GridView cell on click单击时获取 GridView 单元格的字符串值
【发布时间】:2015-04-21 18:05:54
【问题描述】:

我有显示 MySQL 数据库中的一些值的 DataGrid。我需要,如果我单击任何列,该值将保存到字符串。有可能吗?

这是在数据网格中显示 mysql 表的代码

try
{
    conn.Open();
    MySqlCommand cmd = new MySqlCommand("Select Jméno from info", conn);
    MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds, "LoadDataBinding");
    dataGridCustomers.DataContext = ds;
}
catch 
{
    WarnWindow vv1 = new WarnWindow(1);
    vv1.ShowDialog();
}
finally
{
    conn.Close();
}    

但是如果我点击(例如)Bohumil Homola,这个值将被保存为:

string name = Bohumil Homola (column value);

【问题讨论】:

  • “保存到字符串”是什么意思?我们可以看看一些示例代码吗??
  • 我修正了一些错别字并对您的问题进行了细微的文体更改。我重新缩进了你的代码,使它看起来更有条理。而且我还修改了您的标题以(我认为)更好地反映您的目标,并从中删除了“C# WPF”,因为人们普遍认为,如果标签设置正确,标题中不需要它们。祝你好运。

标签: c# mysql wpf datagrid


【解决方案1】:

我终于明白了。这是我使用的代码:

DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)datagridname
    .ItemContainerGenerator
    .ContainerFromIndex(datagridname.SelectedIndex);
DataGridCell RowColumn = datagridname.Columns[0].GetCellContent(row).Parent as DataGridCell;
string ContentOfCell = ((TextBlock)RowColumn.Content).Text;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多