Click here to Skip to main content
15,790,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have developed a customDataGridView for my project. In its OnEnter event , i have tried to set the first cell of first row and first column in editable mode as soon as it gets focus . It is declared as follows.
C#
public partial class CustomControl1 : DataGridView
{
    public CustomControl1()
    {
        this.KeyDown += new KeyEventHandler(CustomDataGridView_KeyDown);       
        InitializeComponent();
    }

  protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        this.CurrentCell = this.Rows[0].Cells[0];
        this.BeginEdit(true);
    }


now the problem is that when I run the debugger, I found out that the code in OnEnter is running twice. what I mean is when the debugger hits
customControl11.Focus();
on form2.cs, it calls the onenter event of customdatagridview ,which first selects the first cell of first row and first column and then set that cell in editable mode by when it hits
this.BeginEdit(true);
. which in turn calls the
customControl11_EditingControlShowing
Till here everything Ok . but now what happens is when it finishes the code block of
customControl11_EditingControlShowing
, it again goes to the OnEnter event of the customcontrol1 and runs the code block again
I can't understand the logic behind it. If any respected member catches my error, please suggest the way to stop it.

What I have tried:

 public partial class Form2 : Form
    {
        private DataSet dataSet;
        private DataView dataView;
        public Form2()
        {
            InitializeComponent();         
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            customControl11.Columns[3].ReadOnly = true;
            dataGridView1.Visible = false;
            dataGridView1.AllowUserToAddRows = false;
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Data Source =DESKTOP-197P3FU\\SQLEXPRESS;Integrated security=SSPI;database=Tally";
               SqlDataAdapter adapter = new SqlDataAdapter($"SELECT * FROM ledgernames", conn);                
                dataSet = new DataSet();
                adapter.Fill(dataSet);
            }
            dataGridView1.DataSource = dataSet.Tables[0];
        }
private void customtextbox3_KeyDown(object sender, KeyEventArgs e)
        {
          switch (e.KeyCode)
                {
                <pre> case Keys.Enter:
                        {
                            if (dataGridView1.Rows.Count > 0)
                            {
                                dataGridView1.Visible = false;
                                customtextbox3.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                                customControl11.Focus();
                                dataView.RowFilter = ""; // resets the filter to its starting state 
                            }
                            e.Handled = e.SuppressKeyPress = true;
                            break;
                        }
                }
            }
        }

C#
private void customControl11_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
if (customControl11.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
            {
               TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.TextChanged -= TextBox_TextChanged; // Attach TextChanged event handler
                    textBox.KeyDown -= TextBox_KeyDown; // Attach KeyDown event handler
                    textBox.Enter -= TextBox_Enter;
                    textBox.TextChanged += TextBox_TextChanged; // Attach TextChanged event handler
                    textBox.KeyDown += TextBox_KeyDown; // Attach KeyDown event handler
                    textBox.Enter += TextBox_Enter;

}
}
}
Posted
Updated 30-May-23 2:35am
v2

1 solution

It's the BeginEdit call - it causes a second OnEnter because the now-editable cell gets the focus instead of the whole control, and it's SetFocus that triggers OnEnter, I believe.

Can you prevent it? I don't think so, it kinda makes sense in the light of what BeginEdit actually does, but you'd have to wade through the Reverence Sources for the DGV to be sure: Reference Source[^]
Warning: it's a monolithic file: nearly 30,000 lines of code.
 
Share this answer
 
Comments
Ankit Goel 30-May-23 5:13am    
@OriginalGriff , so what event should i use ?
OriginalGriff 30-May-23 5:34am    
To do what?
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
And what you have shown is presumably just a demo sample - it doesn't look like "production code". :D
Ankit Goel 30-May-23 6:24am    
To do what ?
i have created a customdatagridview which would later be used in a production environment . And if events keeps on repeating , then the logics inside them will also gets repeated , effectively breaking things again and again . Also i found out that textBox.Enter is also repeating twice due to the same problem . If it would continue it will slow down the initial load response time of my data into datagridview . So its a request . Please guide me at which events should i looking at .
OriginalGriff 30-May-23 6:48am    
No, what are you trying to do that you think requires OnEnter?
We don't know what you are using the control for, so we can't suggest anything!
Ankit Goel 30-May-23 7:37am    
I have updated my question . Basically i am trying to make the first cell of first row and first column of datagridview in edit mode when it receives focus and use that cell as a textbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900