Today I had the misery of learning about the quirks of the DataTable.
Basically adding the second row would generate a "NullReferenceException" - "Object reference not set to an instance of an object."
After a while I learnt that unless I specifically undid the changes I made to the default view it would not work correctly and would erroneously error on the Rows.Add line.
The solution was to not only call theTable.Reset() to prevent the exception but also to manually null the property of the default view that was changed (or the rows were not being displayed).
Below is some sample code.
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable theTable = new DataTable();
// Add column.
theTable.Columns.Add("Test");
// Add a sort rule.
theTable.DefaultView.Sort = "Test ASC";
// Add 2 rows.
theTable.Rows.Add("Hi");
theTable.Rows.Add("Hi2");
// Clear the columns (and rows).
theTable.Rows.Clear();
theTable.Columns.Clear();
// Error also occurs with
//theTable.Clear();
//Solution:
//theTable.Reset();
// Without this the view won't update.
//theTable.DefaultView.Sort = null;
// Add a column again.
theTable.Columns.Add("Test");
// Add the row again.
theTable.Rows.Add("Hi");
// This will cause an error!
theTable.Rows.Add("Hi2");
// Show the table.
dataGridView1.DataSource = theTable;
dataGridView1.Refresh();
}
}
}
Searching Google didn't really reveal much about this, hopefully this will save others some time.
Basically adding the second row would generate a "NullReferenceException" - "Object reference not set to an instance of an object."
After a while I learnt that unless I specifically undid the changes I made to the default view it would not work correctly and would erroneously error on the Rows.Add line.
The solution was to not only call theTable.Reset() to prevent the exception but also to manually null the property of the default view that was changed (or the rows were not being displayed).
Below is some sample code.
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable theTable = new DataTable();
// Add column.
theTable.Columns.Add("Test");
// Add a sort rule.
theTable.DefaultView.Sort = "Test ASC";
// Add 2 rows.
theTable.Rows.Add("Hi");
theTable.Rows.Add("Hi2");
// Clear the columns (and rows).
theTable.Rows.Clear();
theTable.Columns.Clear();
// Error also occurs with
//theTable.Clear();
//Solution:
//theTable.Reset();
// Without this the view won't update.
//theTable.DefaultView.Sort = null;
// Add a column again.
theTable.Columns.Add("Test");
// Add the row again.
theTable.Rows.Add("Hi");
// This will cause an error!
theTable.Rows.Add("Hi2");
// Show the table.
dataGridView1.DataSource = theTable;
dataGridView1.Refresh();
}
}
}
Searching Google didn't really reveal much about this, hopefully this will save others some time.
Matthew1471's ASP 


