Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Good day,

I want to programmatically set the auto generated row of dgv using allowusertoaddrows property, so initially here is my code as shown below:
C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (dataGridView1.Rows.Count > 1)
        {
            if (string.IsNullOrEmpty(dataGridView1.CurrentRow.Cells[Column1.Name].Value.ToString()))
            {
                dataGridView1.AllowUserToAddRows = false;
            }
            else
            {
                dataGridView1.AllowUserToAddRows = true;
            }
        }
    }
    catch (NullReferenceException) { }
    catch (InvalidOperationException) { }
    }

So, I want to prevent from automatically adding row of dgv if column1 of current row is empty, even though other columns at that row was modified. So i try that code above but no luck, then i try again another code using editingcontrolshowing as shown below:
C#
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == Column1.Index)
            {
                if (e.Control is TextBox)
                {
                    txtCol1 = e.Control as TextBox;
                    txtCol1.TextChanged -= new EventHandler(txtCol1_TextChanged);
                    txtCol1.TextChanged += new EventHandler(txtCol1_TextChanged);
                }
            }
            else if (dataGridView1.CurrentCell.ColumnIndex == Column2.Index)
            {
                if (e.Control is TextBox)
                {
                    txtCol2 = e.Control as TextBox;
                    txtCol2.TextChanged -= new EventHandler(txtCol2_TextChanged);
                    txtCol2.TextChanged += new EventHandler(txtCol2_TextChanged);
                }
            }
            //else if... textbox event for another column
        }

        private void txtCol2_TextChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == Column2.Index)
            {
                try
                {
                    if (dataGridView1.CurrentRow.Cells[Column1.Name].Value.ToString() == "")
                    {
                        dataGridView1.AllowUserToAddRows = false;
                    }
                    else
                    {
                        dataGridView1.AllowUserToAddRows = true;
                    }
                }
                catch (NullReferenceException) { }
                catch (InvalidOperationException) { }
            }
        }

        private void txtCol1_TextChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == Column1.Index)
            {
                if (txtCol1.Text == "")
                {
                    dataGridView1.AllowUserToAddRows = false;
                }
                else
                {
                    dataGridView1.AllowUserToAddRows = true;
                }
            }
        }

but still this code is not functioning. I need help for this problem. Your help will be greatly appreciated.

Thank you...

H123ardz

Posted

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