9

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:

Brad Rem
  • 6,036
  • 2
  • 25
  • 50
Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82

5 Answers5

5

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());                    
            }
        }
    }
Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10
  • Sorry could not understand you...what do you mean suppress row add? If you mean that the new row should not be added in DataGrid then just Set AllowUserToAddNewRow = false as I mentioned in the first line above – Arif Eqbal Apr 04 '12 at 09:55
  • I want Row should add conditionally – Haider Ali Wajihi Apr 05 '12 at 07:31
  • 2
    I think thats what we are trying to achieve here. The idea is that Rows will never add automatically (you achieve this by setting AllowUserToAddNewRow to False, I have a feeling you are missing this). Once you set this property to false new rows will never add, so how do you then add new rows? You add new row to the underlying datasource and you do that "conditionally" i.e. when say Qty is greater than 0 as I showed in the CellEndEdit ebent handler code above. – Arif Eqbal Apr 05 '12 at 11:27
  • yeh! i have achieved this solution, but in this solution we don't have any NewRow so we can not identify which row is new. but although i have settled my problem with this solution, thank you – Haider Ali Wajihi Apr 05 '12 at 13:41
3

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;
}
Stefan Huy
  • 31
  • 2
1

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

enter image description here

Dragon Warrior
  • 307
  • 3
  • 16
0

I think on the event CellClick you can check in which column you are and then add a new row, something like : DataGridView1.Rows.Add()

Alex
  • 5,971
  • 11
  • 42
  • 80
0

This is how it is achieved.

a) You can check contents of current row in RowLeave event using

String.IsNullOrWhiteSpace(GridPGLog.Rows[e.RowIndex].Cells[0].value.toString())
using (or) Cells[0] || cells[1] || cell[2] || ..

if any error is found, set focus to the error cell and force user to enter data.

DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow;
rowToSelect.Selected = true;
rowToSelect.Cells[0].Selected = true;
this.dgvJobList.CurrentCell = rowToSelect.Cells[0];

b) or you can place a Save button and check all newly added rows using a foreach loop

arvind
  • 1,385
  • 1
  • 13
  • 21