![](/img/trans.png)
[英]How to programmatically add a double-click event to a WPF DataGrid Row?
[英]How to get value of Row Double-Click Row in gridView
我有一个DevExpress
gridView 我想在 gridView 的双击Row
中获取Row
的值。 我尝试了很多但有错误
Error CS1061 'GridView' does not contain a definition for 'CalcHitInfo' and no accessible extension method 'CalcHitInfo' accepting a first argument of type 'GridView' could be found (are you missing a using directive or an assembly reference?)
以及如何处理对行单元格的点击? 如何将 Row 的值放在新窗口中?
private void gridView2_DoubleClick_1(object sender, EventArgs e)
{
try
{
DXMouseEventArgs ea = e as DXMouseEventArgs;
GridView view = sender as GridView;
GridHitInfo info = view.CalcHitInfo(ea.Location);
if (info.InRow || info.InRowCell)
{
string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
}
}
catch (Exception) { }
}
您的问题是如何在 GridView (DevExpress) 中获取 Row Double-Click Row 的值。
参考DevExpress.Win.Design 22.1.6
NuGet 我能够使用最小形式重现您的问题,我相信问题来自sender as GridView
,它似乎评估为 null(因为发件人is GridControl
并且显然不是兼容的参考根据as操作员文档)。
当我正确投射物体时,一切似乎都正常。
private void gridView2_DoubleClick_1(object? sender, EventArgs e)
{
if (
(sender is GridControl control) &&
(control.MainView is GridView gridView) &&
(e is DXMouseEventArgs args))
{
var hittest = gridView.CalcHitInfo(args.Location);
// BTW don't block the double-click event to do this.
BeginInvoke(() =>
{
MessageBox.Show(
text: Animals[hittest.RowHandle].ToString(),
caption: $"DoubleClick on row: {hittest.RowHandle}, column: {hittest.Column.GetCaption()}"
);
});
}
}
我的最小可重现示例使用此代码来设置DevExpress.XtraGrid.GridControl
。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
gridControl.DataSource = Animals;
Animals.Add(new Animal { Name = "Luna", Kind = Kind.Cat });
Animals.Add(new Animal { Name = "Daisy", Kind = Kind.Dog});
gridControl.DoubleClick += onGridControlDoubleClick;
var view = (GridView)gridControl.MainView;
view.OptionsBehavior.Editable = false;
view.Appearance.FocusedCell.BackColor = Color.CadetBlue;
view.Appearance.FocusedCell.ForeColor = Color.White;
}
private void onGridControlDoubleClick(object? sender, EventArgs e)
{
if (
(sender is GridControl control) &&
(control.MainView is GridView gridView) &&
(e is DXMouseEventArgs args))
{
var hittest = gridView.CalcHitInfo(args.Location);
// BTW don't block the double-click event to do this.
BeginInvoke(() =>
{
MessageBox.Show(
text: Animals[hittest.RowHandle].ToString(),
caption: $"DoubleClick on row: {hittest.RowHandle}, column: {hittest.Column.GetCaption()}"
);
});
}
}
public BindingList<Animal> Animals { get; } = new BindingList<Animal>();
}
public enum Kind
{
Other,
Cat,
Dog,
}
public class Animal
{
[Display(AutoGenerateField = false)]
public string ID { get; set; } = Guid.NewGuid().ToString().Substring(0,8);
public string? Name { get; set; }
public Kind Kind { get; set; }
public override string ToString() => $"{Name} ({Kind})";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.