Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3798218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:32:13+00:00 2026-05-19T13:32:13+00:00

I have a DataTable (displayed via a DataGridView) and want to reuse it to

  • 0

I have a DataTable (displayed via a DataGridView) and want to reuse it to manually fill in different data. So the workflow is:

Every time I need to refill the DataTable, I do:

  1. Unhook the DataTable from the DataGridView.
  2. Clear DataTable content.
  3. Manually add DataColumns and DataRows to the DataTable.
  4. Hook the DataTable again with the DataGridView.

To achieve step 0, I simply do: DataGridView.DataSource = null.

To achieve step 1, I call DataTable.Clear(), DataTable.Rows.Clear() and DataTable.Columns.Clear().

To achieve step 3, I call DataGridView.DataSource = DataTable.

Step 2 is done in normal way and I skip the code.

Whenever the DataGridView is updated with a new content, if I click DataGridView column to sort, the next time when the DataTable is updated, I will get a null reference exception in step 2 when I try to add new rows. But if I never click DataGridView column to sort, all works fine.

I guess it has something to do with the sorting. But I have no idea what causes the null reference exception. Is it possible that the DataTable tries to search some keys in old content but couldn’t find it? It cannot find the old content because of step 1 of course. What is the right way of clearing old DataTable content so it never refers the old content when I fill in new stuff?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:32:13+00:00Added an answer on May 19, 2026 at 1:32 pm

    The DataGridView’s databinding is tied to the table (through a DataView); you cannot pull the table out from under it and expect it to continue working.
    When you sort by a column, the grid asks the DataView to change its sort order, which fails because the column doesn’t exist anymoe.

    You should create a new DataTable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have datatable with column name tag and 100 rows of data.I need to
I have a DataTable that I manually created and loaded with data using C#.
i have a datatable with the results i just want to refine the results
I have a DataTable object. Every column is of type string. Using LINQ, how
I have a DataTable as a data source of a GridView. I'm adding a
I have a dataGridView whose dataSource is a dataTable. My problem is that I
Hey guys, I have an application that I want to display some data from
In C#, I have a table displayed using a DataGridView . The table is
I have a SQLite database that is displayed in a data grid view in
I have a list of items. I want an optionsBar to be displayed right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.