如何在 C# DataGrid 中双击一行的任意列时获取行的第一个单元格的内容
How to get the contents of the first cell of a row when double clicked on any column of a row in C# DataGrid
我想在双击一行中的任意单元格时获取任意行第一个单元格的内容。 (例如,如果我在这两种情况下都单击 cell[row1, column2]
或 cell[row1, column3]
,我想获取 cell[row1, column1])
的内容。
使用我编写的代码,我得到了双击单元格的内容。
在我的代码中,var cellValue
为我提供了单元格的内容。所以,我想实现cell[row r, column1]
里面的var cellValue
和private List<DataGrid> m_AllDgTag = new List<DataGrid>();
的内容
我的代码:
private void DgCM_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
int currentPnl = 0;
if (m_AllDgTag.Count > 1)
{
currentPnl = m_CurrentFDS;
}
var cellInfo = m_AllDgTag[currentPnl].CurrentCell;
if (cellInfo != null && m_CurrentStep != null)
{
var column = cellInfo.Column as DataGridBoundColumn;
int cellColIndex = cellInfo.Column.DisplayIndex;
if (column != null)
{
var element = new FrameworkElement() { DataContext = cellInfo.Item };
BindingOperations.SetBinding(element, FrameworkElement.TagProperty,
column.Binding);
var cellValue = element.Tag;
...
}
}
}
我附上物品阵列的图片。我能够获取 'var rowinfo' 中行的数据。从这里我想获取列 [0] 的数据。所以在这种情况下,我想获取值,例如列 [0] 值:F21B1C001.enter image description here
哈桑,在你发表评论后我了解到你的代码是一个 WPF 应用程序。
我在 WPF 中创建了一个示例
这是网格的XAML,解法的关键是
DataGrid.Resources Style TargetType="DataGridRow"
<Grid>
<DataGrid HorizontalAlignment="Left"
Height="203"
Margin="175,126,0,0"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{ Binding Persons }"
VerticalAlignment="Top"
Width="378" >
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
这是背后的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
var dbClickedRow = (DataGridRow)sender;
MessageBox.Show(((Person)dbClickedRow.DataContext).Name);
}
public List<Person> Persons
{
get
{
return new List<Person>
{
new Person{ Name = "Gustavo", LastName = "Oliveira", Age = 35 },
new Person{ Name = "Another", LastName = "Person", Age = 23 },
new Person{ Name = "Neymar", LastName = "Junior", Age = 28 },
};
}
}
}
public class Person
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
我找到了问题的解决方案。如果我们使用这段代码,我们将获得单击的任何行的第 [0] 列的值。
var cellValue = ((System.Data.DataRowView)cellInfo.Item).Row.ItemArray[0];
感谢@Gustavo Oliveira 的帮助
我想在双击一行中的任意单元格时获取任意行第一个单元格的内容。 (例如,如果我在这两种情况下都单击 cell[row1, column2]
或 cell[row1, column3]
,我想获取 cell[row1, column1])
的内容。
使用我编写的代码,我得到了双击单元格的内容。
在我的代码中,var cellValue
为我提供了单元格的内容。所以,我想实现cell[row r, column1]
里面的var cellValue
和private List<DataGrid> m_AllDgTag = new List<DataGrid>();
我的代码:
private void DgCM_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
int currentPnl = 0;
if (m_AllDgTag.Count > 1)
{
currentPnl = m_CurrentFDS;
}
var cellInfo = m_AllDgTag[currentPnl].CurrentCell;
if (cellInfo != null && m_CurrentStep != null)
{
var column = cellInfo.Column as DataGridBoundColumn;
int cellColIndex = cellInfo.Column.DisplayIndex;
if (column != null)
{
var element = new FrameworkElement() { DataContext = cellInfo.Item };
BindingOperations.SetBinding(element, FrameworkElement.TagProperty,
column.Binding);
var cellValue = element.Tag;
...
}
}
}
我附上物品阵列的图片。我能够获取 'var rowinfo' 中行的数据。从这里我想获取列 [0] 的数据。所以在这种情况下,我想获取值,例如列 [0] 值:F21B1C001.enter image description here
哈桑,在你发表评论后我了解到你的代码是一个 WPF 应用程序。
我在 WPF 中创建了一个示例
这是网格的XAML,解法的关键是
DataGrid.Resources Style TargetType="DataGridRow"
<Grid>
<DataGrid HorizontalAlignment="Left"
Height="203"
Margin="175,126,0,0"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{ Binding Persons }"
VerticalAlignment="Top"
Width="378" >
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
这是背后的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
var dbClickedRow = (DataGridRow)sender;
MessageBox.Show(((Person)dbClickedRow.DataContext).Name);
}
public List<Person> Persons
{
get
{
return new List<Person>
{
new Person{ Name = "Gustavo", LastName = "Oliveira", Age = 35 },
new Person{ Name = "Another", LastName = "Person", Age = 23 },
new Person{ Name = "Neymar", LastName = "Junior", Age = 28 },
};
}
}
}
public class Person
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
我找到了问题的解决方案。如果我们使用这段代码,我们将获得单击的任何行的第 [0] 列的值。 var cellValue = ((System.Data.DataRowView)cellInfo.Item).Row.ItemArray[0];
感谢@Gustavo Oliveira 的帮助