3

I'm working on a desktop application in C# (.NET 4.0). I have a datagridview filled with custom objects, via custom (it inherits BindingList) BindingList (added sorting functionalities). I use cellValidating events to properly increment first column (ID) and to validate input into other cells.

The problem is when I get to the new row / last row and enter something into one cell (I can even delete all the input before leaving the cell) the datagridview automatically adds this row to binding list and creates a new row below. I want that the last / new row is only added when the row above (previous new row) is properly filled.

Example

ID     NAME      PRICE    ...

1      Beer       2.6

2      Cheese      3.3

_      _______     ____      <- empty row

Now if I click (enter) the new row ID is automatically set to 3 and if I leave click to 'Beer' cell, new row ID is empty and all default values are empty (which is the way it should & does work). The problem is if if click / enter new row and type something for name (and even if I delete the content before leaving cell) a new row is added below this row, which was new row. Thus this row is added to BindingList and is incomplete, it shouldn't be added until all required cells are properly filled (for instance price).

I have no idea how to do this. Please help me out, I'm quite new to C#.

Thank you for your time and answers.

AMadmanTriumphs
  • 4,888
  • 3
  • 28
  • 44
Ben
  • 2,435
  • 6
  • 43
  • 57

2 Answers2

3

You have to handle the RowValidating event and cancel the handling of the event after checking specific conditions (validation rules). Then, the currently being entered row will not be committed to the DataGridView and no new row will be added.

For example:

if (dgv[0, e.RowIndex].Value == DBNull.Value)
{
    dgv.Rows[e.RowIndex].ErrorText = "You must enter a value for this field!";

    // Tell the DataGridView not to accept this row
    e.Cancel = true;
}
Ken D
  • 5,880
  • 2
  • 36
  • 58
0

If you are developing a windows application and have textboxes to get input from user to update to the DataGridView, you can do it in C# itself, consider the following:

if(txtNAME.Text.Trim()!=String.Empty&&txtPRICE.Text.Trim()!=String.Empty)
{
  MessageBox.Show("Invalid type of input","Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

else
{
  SqlConnection _sqlconnectionOne=new SqlConnection(*DatabaseConnectionString*);

  SqlCommand _sqlcommandOne=new SqlCommand();

  _sqlcommandOne.CommandType=CommandType.Text;
  _sqlcommandOne.CommandText="*INSERTStatement*";
  _sqlcommandOne.Connection=_sqlconnectionOne;
  _sqlcommandOne.ExecuteNonQuery();

  SqlConnection _sqlconnectionSecond=new SqlConnection(*DatabaseConnectionString*);

  SqlCommand _sqlcommandSecond=new SqlCommand();

  _sqlcommandSecond.CommandType=CommandType.Text;
  _sqlcommandSecond.CommandText="*SELECT*Statement*";
  _sqlcommandSecond.Connection=_sqlconnectionSecond;

  SqlDataAdapter _sqldataadapter=new SqlDataAdapter(_sqlcommandSecond);

  DataTable _datatable=new DataTable();

  _sqldataadapter.fill(_datatable);
  *DataGridViewName*.DataSource=_datatable;
}

Or you can use the Leave Event for the textboxes but it becomes a little hustle sometimes. Suggestions are always welcomed!

Asif Patankar
  • 97
  • 4
  • 15