Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing a new row from appearing in a DataGridView before current row is filled in?

I have a DataGridView in my WinForm application in C# 3.5.

AllowUserToAddNewRow property is set true. When user types any text in DataGridView, another new row is automatically added to DataGridView. I do not want this new row added until some checks are performed on the current row and all the necessary information has been filled in there.

Example : I have a DataGridView with a blank row: DataGridView with one blank row

When I begin typing a new row is added, which is too soon:

What I want is that the new row be added only after the user enters a Quantity:

like image 939
Haider Ali Wajihi Avatar asked Apr 03 '12 06:04

Haider Ali Wajihi


People also ask

How to disable auto generated rows in DataGridView?

Simply set the DataGridView. AllowUserToAddRows property to false .

What is DataGridViewRow?

The DataGridViewRow class represents a row in a DataGridView control. You can retrieve rows through the Rows and SelectedRows collections of the control. Unlike a DataGridViewColumn, a DataGridViewRow physically contains a collection of all of the cells in that row.

How to disable DataGrid column in c#?

Set the column's ReadOnly property to true to make it non-editable. And alter its DefaultCellStyle. BackColor (and/or ForeColor) to make it obvious to the user.


3 Answers

Set AllowUserToAddNewRow = false Now add a blank row initially to your datasource for eg. if you are binding the DataGridView to a DataTable called DT then just before

dataGridView1.DataSource = DT;

Do something like

 DT.Rows.Add(DT.NewRow());

This is to have one blank row initially so that the first record can be entered. Then handle the event dataGridView1.CellEndEdit, in that event write something like this:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 1)//The index of your Quantity Column
        {
            int qty = (int)DT.Rows[e.RowIndex][e.ColumnIndex];
            if (qty > 0)//Your logic if required
            {
                DT.Rows.Add(DT.NewRow());                    
            }
        }
    }
like image 120
Arif Eqbal Avatar answered Oct 20 '22 23:10

Arif Eqbal


Basically it's a simple play with some events and enabling/disabling the AllowUserToAddRow property:

public Form1()
        {
            InitializeComponent();
            //creating a test DataTable and adding an empty row
            DataTable dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Columns.Add("Column2");
            dt.Rows.Add(dt.NewRow());

            //binding to the gridview
            dataGridView1.DataSource = dt;

            //Set  the property AllowUserToAddRows to false will prevent a new empty row
            dataGridView1.AllowUserToAddRows = false;
        }

Now the events... When the cell recognize the editing it will fire a event called CellBeginEdit. When it's in editing mode set the AllowUserToAddRows to false

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    dataGridView1.AllowUserToAddRows = false;
}

When the cell recognize the end of editing it will fire a event called CellEndEdit. When it ends the editing mode check for your conditions. Based on the result set the AllowUserToAddRows to true ot keep it false.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    //instead of MessageBox there could be as well your check conditions
    if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        dataGridView1.AllowUserToAddRows = true;
    else dataGridView1.AllowUserToAddRows = false;
}
like image 23
Stefan Huy Avatar answered Oct 20 '22 23:10

Stefan Huy


I know this is old. The easiest way is, uncheck "Enable Adding" from the design view

enter image description here

like image 33
Dragon Warrior Avatar answered Oct 20 '22 22:10

Dragon Warrior