Dataentry Datagridview

ankit goel 766 Reputation points
2023-07-04T13:09:23.74+00:00

I have created a similar custom datagridview like https://learn.microsoft.com/en-us/answers/questions/1321587/enter-key-as-tab-not-working-properly-in-datagridv

protected override void OnEnter(EventArgs e)
    {
        // Set the current cell to the first cell and enable editing when the control receives focus
        if (RowCount > 0 && ColumnCount > 0)
        {
            CurrentCell = Rows[0].Cells[0];
            BeginEdit(true);
        }

        base.OnEnter(e);
    }

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            // Move the focus to the next column and row when Enter key is pressed
            if (CurrentCell.ColumnIndex == ColumnCount - 1)
            {
                if (CurrentCell.RowIndex == RowCount - 1)
                {
                    // Reached the last cell, move focus to the first cell
                    CurrentCell = Rows[0].Cells[0];
                }
                else
                {
                    // Move to the first cell of the next row
                    CurrentCell = Rows[CurrentCell.RowIndex + 1].Cells[0];
                }
            }
            else
            {
                // Move to the next column
                CurrentCell = Rows[CurrentCell.RowIndex].Cells[CurrentCell.ColumnIndex + 1];
            }

            BeginEdit(true);

            return true;
        }

        return base.ProcessDialogKey(keyData);
    }


What I have done is created another datagridview (on inheriting page) which would be used for giving matching items and would be initially hidden and only becomes visible when the focus reaches this currentdatagridview's first cell . The problem is I want to modify the behaviour of arrow keys inside datagridview . Here's what i am trying

  1. if i am inside the column0 and typing something , if i presses up , down key there , it should not move down . instead i should be able to move up and down the results of that hidden datagridview .

please suggest me how to get this

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,919 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,236 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,856 Reputation points Microsoft Vendor
    2023-07-06T10:15:35.2666667+00:00

    Hi @ankit goel , Welcome to Microsoft Q&A.

    Here is the custom datagridview: Which creates a mock list of the database (do the replacements you need).

    Change the data source of suggestdata2 to list2.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    namespace_7_3_cc1
    {
        public partial class CustomDataview2: DataGridView
        {
            public DataGridView suggestivedatagridview;
            public class Product
            {
                public string Name
                {
                    get;
                    set;
                }
                public decimal Price
                {
                    get;
                    set;
                }
            }
            // Define a List<Product> as a data source in the form class
            public List < Product > products = new List < Product > ();
            private List < Product > products2 = new List < Product > ();
            //
            int rowNowNumber = -1;
            int rowMaxNuber = -1;
            public CustomDataview2()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
            protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
            {
                base.OnCellValueChanged(e);
                if(this.SelectedCells.Count > 0)
                {
                    DataGridViewCell dataGridCell = this.SelectedCells[0];
                    if(dataGridCell.ColumnIndex == 0 && dataGridCell.RowIndex >= 0) // Assume the string to be filtered is in the first column
                    {
                        if(dataGridCell.Value != null)
                        {
                            suggestivedatagridview.Visible = true;
                            string filterString = this.Rows[dataGridCell.RowIndex].Cells[dataGridCell.ColumnIndex].Value.ToString();
                            // Clear the previous filter results
                            products2.Clear();
                            // Filter the products list and add qualified data to the products2 list
                            products2 = products.Where(p => p.Name.Contains(filterString)).ToList();
                            // Bind the new filter result to dataGridView2
                            suggestivedatagridview.DataSource = products2;
                            suggestivedatagridview.CurrentCell = null;
                            if(products2 != null)
                            {
                                suggestivedatagridview.Rows[0].Selected = true;
                                rowNowNumber = 0;
                                rowMaxNuber = suggestivedatagridview.Rows.Count;
                            }
                            CurrentCell = dataGridCell;
                            BeginEdit(true);
                        }
                    }
                }
            }
            protected override void OnEnter(EventArgs e)
            {
                // Set the current cell to the first cell and enable editing when the control receives focus
                if(RowCount > 0 && ColumnCount > 0)
                {
                    CurrentCell = Rows[0].Cells[0];
                    BeginEdit(true);
                }
                base.OnEnter(e);
            }
            protected override bool ProcessDialogKey(Keys keyData)
            {
                DataGridViewCell dataGridCell = this.SelectedCells[0];
                if(keyData == Keys.Enter)
                {
                    this.CommitEdit(DataGridViewDataErrorContexts.Commit);
                    return true;
                }
                return base.ProcessDialogKey(keyData);
            }
            protected override bool ProcessDataGridViewKey(KeyEventArgs e)
            {
                DataGridViewCell dataGridCell = this.SelectedCells[0];
                if(IsCurrentCellInEditMode)
                {
                    if(e.KeyCode == Keys.Up)
                    {
                        // CurrentCell = dataGridCell;
                        if(!suggestivedatagridview.Rows[0].IsNewRow)
                        {
                            if((rowNowNumber - 1) >= 0)
                            {
                                suggestivedatagridview.Rows[rowNowNumber].Selected = false;
                                suggestivedatagridview.Rows[--rowNowNumber].Selected = true;
                            }
                        }
                        BeginEdit(true);
                        return true;
                    }
                    else if(e.KeyCode == Keys.Down)
                    {
                        //CurrentCell = null;
                        // CurrentCell = dataGridCell;
                        if(!suggestivedatagridview.Rows[0].IsNewRow)
                        {
                            if((rowNowNumber + 1) < rowMaxNumber)
                            {
                                suggestivedatagridview.Rows[rowNowNumber].Selected = false;
                                suggestivedatagridview.Rows[++rowNowNumber].Selected = true;
                            }
                        }
                        BeginEdit(true);
                        return true;
                    }
                }
                return base.ProcessDataGridViewKey(e);
            }
        }
    }
    

    Form1: After dragging into the custom datagridview, you need to pass in suggestdata2 and data in the code page. Add the required column by yourself.

    using System.Collections.Generic;
    using System.Windows.Forms;
    namespace_7_6_1
    {
        public partial class Form1: Form
        {
            // Define a List<Product> as a data source in the form class
            private List < _7_3_cc1.CustomDataview2.Product > products = new List < _7_3_cc1.CustomDataview2.Product > ();
            public Form1()
            {
                InitializeComponent();
                // add some sample data
                products.Add(new _7_3_cc1.CustomDataview2.Product
                {
                    Name = "ranjan", Price = 10.99 m
                });
                products.Add(new _7_3_cc1.CustomDataview2.Product
                {
                    Name = "abhigyan", Price = 20.50 m
                });
                products.Add(new _7_3_cc1.CustomDataview2.Product
                {
                    Name = "tunjan", Price = 15.75 m
                });
                products.Add(new _7_3_cc1.CustomDataview2.Product
                {
                    Name = "xxxxx", Price = 20.75 m
                });
                // hide d2
                suggestivedatagridview.Visible = false;
                customDataview21.products = products;
                customDataview21.suggestivedatagridview = suggestivedatagridview;
            }
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.