简体   繁体   中英

How to get value of Row Double-Click Row in gridView

I have a DevExpress gridView I want to get value of Row in Double-Click Row in gridView. I tried a lot but there error

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?)

as well How to handle clicks on row's cells? How to i put value of Row in New window?

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) { }

}

Your question is How to get value of Row Double-Click Row in GridView (DevExpress).

Referencing DevExpress.Win.Design 22.1.6 NuGet I was able to reproduce your issue using a minimal form and I believe the problem is coming from sender as GridView which seems to evaluate null (because the sender is GridControl and evidently is not a compatible reference per the as operator documentation).

When I cast the objects correctly, everything seems to work.

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()}"
            );
        });
    }
}

双击响应


My minimal reproducible example uses this code to set up the 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})";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM