Object Reference not found after sorting a column

I get "Object Reference not found" error after sorting a column in datagridview control
if I initial the DataTable this way:
                vDGV.DTable.Clear();
                vDGV.DTable.Columns.Clear();
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                SqlDataAdapter mDA = new SqlDataAdapter(mCmd);
                mDA.Fill(vDGV.DTable);
                dataGridView1.DataSource = vDGV.DTable;
but if I changed the first two lines to
                vDGV.DTable = new DataTable();  // changed
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                SqlDataAdapter mDA = new SqlDataAdapter(mCmd);
                mDA.Fill(vDGV.DTable);
                dataGridView1.DataSource = vDGV.DTable;
I am OK.

Question is, how should I initialise the DataTable to prevent the object not found reference?


sepknowAsked:
Who is Participating?
 
JimBrandleyCommented:
If you are loading from the same table each time, try DataTable.Clear(), but leave the columns and constraints collections alone.

There is a lot of stuff in a DataTable in addition to columns and rows. I suspect the clears we have tried so far is leaving an invalid reference.

Jim
0
 
Jai STech ArchCommented:
is this coding in your SORTING part ?
if yes, you dont need to set the DATASOURCE each and every time it is sort !!

vDGV.DTable = new DataTable() - this statement itself will create a new object of your datatable...you dont need to .clear the rows and columns...
0
 
dunglaCommented:
vDGV.DTable.Clear(); will clear the data and keep structure of DataTable. When you try to sorting gridview maybe the column does not exist?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
sepknowAuthor Commented:
I just allowed sorting in some of the columns by setting the column SortMode to DataGridViewColumnSortMode.Automatic.

The error occurred when executing reach the line:
mDA.Fill(vDGV.DTable);

0
 
JimBrandleyCommented:
Try:
vDGV.DTable.Constraints.Clear();
vDGV.DTable.Clear();
vDGV.DTable.Columns.Clear();

You may have constraints complainig when the columns collection is cleared.

Jim
0
 
sepknowAuthor Commented:
I still get the same error.
0
 
Jai STech ArchCommented:
can you tell in which line you are getting the error ?
0
 
Jai STech ArchCommented:
you need to add
vDGV.DTable = new DataTable();  // changed
before you start filling the data table...
0
 
sepknowAuthor Commented:
Hi Jim,
This helps.
Thanks.
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.