Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > unhandled Exception thrown when user enters value in a new row in datagridView
 

unhandled Exception thrown when user enters value in a new row in datagridView

I have a datagridView in my screen which is bounded to a dataSource which has been generated through DataSource Wizard.DataError event is added for handling invalid data. Consider, if user enters invalid data in very first row of datagridview and tabs out from the invalid data column, then dataerror message is displayed. Now if user hits Esc key to clear invalid contents, system throws an exception which is what we cannot figure out.
 
This is particular only if datagridview is empty when form loads else if there is any saved row(s)..no issues as such.
Jency

pjencyjoy  Monday, December 12, 2005 6:51 AM
This is a bug in the DataGridView specifically because the first column is hidden. You can get around this by deriving from the DGV and overridding the SetCurrentCellAddressCore method like so:

protected override bool SetCurrentCellAddressCore(...)
{
    if (columnIndex == 0) columnIndex = 1;
    return base.SetCurrentCellAddressCore(...);
}

 
I've opened a bug to track this.
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Wednesday, December 14, 2005 2:15 AM
I'll look at this today or tomorrow. Can you provide comments to the type of constraints you have on your table? For example, any non-null columns or unique constraints?

thanks,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Monday, December 12, 2005 8:25 PM
I dont have any specific constraints as such on any column except the primary key.And the primary key field is an identity column.Rest all columns as "Nullable".

Since the primary key field is an identity column and data is not entered in it through the grid we commented the "Unique constraint" check in the designer code of the dataset , generated through dataSource wizard.

I am getting this exception "Object reference not set to an instance" in the designer code of dataset where it is trying to add a new row.
pjencyjoy  Tuesday, December 13, 2005 11:00 AM
This is a bug in the DataGridView specifically because the first column is hidden. You can get around this by deriving from the DGV and overridding the SetCurrentCellAddressCore method like so:

protected override bool SetCurrentCellAddressCore(...)
{
    if (columnIndex == 0) columnIndex = 1;
    return base.SetCurrentCellAddressCore(...);
}

 
I've opened a bug to track this.
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Wednesday, December 14, 2005 2:15 AM

Mark,

The problem still seems to be occuring even when the first column is not hidden, and after I tried overriding SetCurrentCellAddressCore(), as you suggested. Here's some test code illustrating how to reproduce the exception.

Is it still a bug, or just something I am doing wrong in my experiment, please?

Thank you.

Steps:

  1. Create a standard WindowsApplication, call it "SetCurrentCellAddressCore".
  2. Add a new DataSet item to the project (that'll be our DataSet1 type).
  3. Add three tables with two columns each (accept default names for both table and column names) to the DataSet (using the DataSet editor, in DataSet1.xsd).
  4. Place a TreeView and a DataGridView on the form.
  5. Paste the code below into the Form1.cs file (within the namespace brackets).
  6. Compile and run the app.
  7. Click on Node1.
  8. Click on the cell displaying text “DataTable1�
  9. Press “Enter�
  10. Get the exception.

public partial class Form1 : Form

{

private DataSet1 _dataSet = new DataSet1();

private CurrencyManager _RowManager;

public Form1()

{

InitializeComponent();

treeView1.Nodes[0].Nodes[0].Tag = _dataSet.Tables[0];

treeView1.Nodes[0].Nodes[1].Tag = _dataSet.Tables[1];

treeView1.Nodes[0].Nodes[2].Tag = _dataSet.Tables[2];

}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

{

if (e.Node.Tag != null)

{

ChangeTable(e.Node.Tag as DataTable);

}

}

private void ChangeTable(DataTable newTable)

{

dataGridView1.DataSource = newTable;

if (_RowManager != null)

{

_RowManager.PositionChanged -= new EventHandler(cursor_PositionChanged);

}

_RowManager = BindingContext[newTable] as CurrencyManager;

_RowManager.PositionChanged += new EventHandler(cursor_PositionChanged);

if (newTable.Rows.Count == 0)

{

newTable.Rows.Add(newTable.TableName, newTable.TableName);

}

}

void cursor_PositionChanged(object sender, EventArgs e)

{

if (-1 == _RowManager.Position)

{

return;

}

if ((_RowManager.Current as DataRowView).IsNew)

{

try

{

//We only want to respond when the user scrolls to the next row in dataGridView.

if (!treeView1.Focused)

{

TreeNode current = treeView1.SelectedNode;

//Select next treeNode (in a wrap-around fashion).

if (current.Parent != null && current.Parent.GetNodeCount(false) > 1)

{

TreeNode sibling = current.NextNode;

if (sibling != null)

{

treeView1.SelectedNode = sibling;

}

else

{

treeView1.SelectedNode = current.Parent.Nodes[0];

}

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}

Novozh  Thursday, March 23, 2006 3:39 PM
Probably stating the obvious but an easy solution for us was to move the hidden column to the end of the column list in the DataGridView.

- Rory
Rory K  Monday, April 30, 2007 12:27 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridViewComboBoxColumn Problem
• How do i add new record to DB using detail form - not datagrid?
• how to make gridview to change datasource
• ... cannot be set on a data-bound DataGridView control
• Object not showing in "Add New Data Source"
• Optimization DataSet
• Creating an SQL Express Database and two datatables at runtime
• How to Display the child data in a combobox which is in a DataagridView
• Newbie Question: How to get my C# application to connect to several databases?
• Strategies for Showing / Notifying Users of New Data?