DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException  
Author Message
Andreas Kranister





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

Since I work with .net 2.0 databinding I run into one problem after the other. Unfortunatly things that should work in a nice way only do on the surface but not in detail. At least that is my impression. The AddItem Button of the BindingNavigator seems to be unusable. I hope I am wrong and there is a way to solve the problem.

To reproduce the problem:
1. Add a DataGridView and a BindingNavigator to a Form
2. Bind both to a BindingSource
3. The DataSource of the BindingSource is a DataTable that contains at least 1 column where AllowDBNull = false
4. Run the Application
5. Press the AddNewItem Button in the BindingNavigator
6. Press the AddNewItem Button in the BindingNavigator
7. NoNullAllowedException occurs

I did not find a way to catch and handle this Exception, so I disabled the built in beavior of the BindingNavigator and handled the click event of the button (see also the thread http://www.hide-link.com/ ;SiteID=1&mode=1):
private void OnAddNewClick(object sender, EventArgs e)
{
DataRowView r;
try
{
r = myKeySource.AddNew() as DataRowView;
}
catch (NoNullAllowedException eX)
{
..
}
catch (ConstraintException eX)
{
..
}
}

Now the problem seemed to be solved. But an other one occured. If I first add a row by using the BindingNavigator (that uses my code above) and then use the "new row line" by clicking it, the NoNullAllowedException occurs again. I am not able to find a way to solve this problem.

Please help by a direct answer or a link that handles these bugs.

Thank you very much,
Andreas



Windows Forms19  
 
 
hrubesh





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

What is happening is that your current new row needs a primary key, and some other columns that do not allow null, now if you try add a new row, you get the error, because the current row will be the new row, When moving from one row to another you get:

Cell Leave (old cell), Row leave (old row)

Cell Validating/ed (old cell)

Cell EndEdit (old cell)

Row Validating/ed (old row)

Row Enter (new row)

Cell Enter (new cell)

Cell BeginEdit (new cell)

so your old row is not valid,. thereby the nullerror you getting.

so you can handle that by handling the defaultvaluesneeded event

private void dataGridView1_DefaultValuesNeeded(object sender,

DataGridViewRowEventArgs e)

{

e.Row.Cells["Country"].Value = "USA";

e.Row.Cells["CustomerID"].Value = NewCustomerId();

}

"When the user selects the row for new records as the current row, the DataGridView control raises the DefaultValuesNeeded event. This event provides access to the new DataGridViewRow and enables you to populate the new row with default data." Extract from DGV FAQ.



 
 
Andreas Kranister





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

Thanks for your answer. Your solution does not work. The exception occurs before the event DefaultValuesNeeded gets triggered.


 
 
hrubesh





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

lol yep its true i tried it all.

nothing actually gets triggered except row validating but i checked isnewrow at this point and it is false,

'Cell Leave (old cell), Row leave (old row)

'Cell Validating/ed (old cell)

'Cell EndEdit (old cell)

'Row Validating/ed (old row)

'Row(Enter(New row))

'Cell(Enter(New cell))

'Cell(BeginEdit(New cell))

nor defaultvaluesneeded actually is working in mine too.

you are working with datatables

i actually always handled this part in my datatable new row event.

Private Sub dt1_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles dt1.TableNewRow

e.Row.Item("pono") = Me.Pono

End Sub

I would not consider the problem to be with the add button of the navigator and go so far and handle nonull exception

in a solution.

because it does make sense that when adding a new new row, the old new row is committed,

then obviously the table does not allow nulls does it



 
 
Andreas Kranister





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

Thanks again for your answer. To reproduce the problem please do the following steps:
1. Create a Visual C# Windows Application project in VS 2005
2. Delete the class Form1.cs
3. Delete the class Program.cs
3. Add a new class
4. Copy the following code in this class

// begin of code
using System;
using System.Data;
using System.Windows.Forms;

namespace Test
{
public class TestForm : Form
{
public TestForm()
{
DataSet ds = new DataSet("TestDataSet");
DataTable dt = new DataTable("TestDataTable");
ds.Tables.Add(dt);

DataColumn key = new DataColumn("Key");
key.AllowDBNull = false;
key.Unique = true;
key.DataType = typeof(string);
dt.Columns.Add(key);

DataColumn data = new DataColumn("Data");
key.DataType = typeof(string);
dt.Columns.Add(data);

BindingSource bs = new BindingSource(ds, "TestDataTable");

BindingNavigator nav = new BindingNavigator(false);
nav.Dock = DockStyle.Top;
nav.BindingSource = bs;
this.Controls.Add(nav);

DataGridView dv = new DataGridView();
dv.Dock = DockStyle.Fill;
dv.DataSource = bs;
this.Controls.Add(dv);

ToolStripButton addButton = new ToolStripButton("New");
nav.Items.Add(addButton);
nav.AddNewItem = addButton;
}

static void Main()
{
Application.Run(new TestForm());
}
}
}
// end of code

Suggest the value of the column "Key" is not deterministic, it can not be calculated and must be entered by the user.
It is not possible to define a default value for this column.

How to catch and handle the exceptions ocurring at this situations:
1. press the button "New" then press the button "New" again
2. press the button "New" then click into the "* row" to add a new row
3. enter a value in the second column (data), e.g. 'a', press the button "New", click "Ok" in the occuring exception message, press Escape


I think the only way to handle this case in a correct manner is to cancel adding a new row if the key column of the current row is not valid.
I tried several ways (BindingSource.AddingNew, DataTable.TableNewRow, DataGridView.Validating, DataGridView.DataError, ...) but was never able to handle all three cases above. In some cases new troubles occure.

Begging for a solution,
Andreas


 
 
DamienCahill





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

Has anyone found a solution to this problem yet

I have looked into the walkthrough at

http://msdn2.microsoft.com/en-us/library/ms171930(VS.80).aspx but it does not seem to solve this problem as the EndInit() event that is overriden is not raised before the error that causes the nullvalue exception.

I too am begging for a solution.

Thanks,

Damien.


 
 
Roger Scudder





PostPosted: Windows Forms Data Controls and Databinding, DataGridView, BindingNavigator: Adding new lines, NoNullAllowedException Top

Best solution so far is to disable constraints at the DataSet level. For example...

ds.EnforceConstraints = false;

Of course you should not disable constraints at the datasource level also, or you may suffer database corruption.