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 7806839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:39:40+00:00 2026-06-02T02:39:40+00:00

what i am trying to do is write a sepprate function that is in

  • 0

what i am trying to do is write a sepprate function that is in the business logic of my Windows form application. this function recieves the xml file path and the datagridview object as arguments.

This is supposed to work:

DataTable dt = new DataTable();
dt = (DataTable)datagridview.DataSource;
DataSet ds = new DataSet(); 
ds.Tables.Add(dt);
ds.WriteXml(xml_file, System.Data.XmlWriteMode.IgnoreSchema);

But i have a error everytime with the line:

dt = (DataTable)datagridview.DataSource;

[System.InvalidCastException] = {"Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'."}

so i was advised in another blog to try this:

BindingSource bs = (BindingSource)dgv.DataSource;
dt = (DataTable)bs.DataSource;

but i get

[System.InvalidCastException] = {"Unable to cast object of type 'ExportDataTestApp.NorthwindDataSet' to type 'System.Data.DataTable'."}

i have searched and tryed everything i would not be supprised if it is something simple as i am new to c# but i need help please

  • 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-06-02T02:39:41+00:00Added an answer on June 2, 2026 at 2:39 am

    You’re almost there.

    BindingSource bs = (BindingSource)dgv.DataSource;
    DataSet ds = (DataSet)bs.DataSource;
    DataTable dt = ds.Tables["Customers"];  // or Tables[0]
    

    Apparently you are binding with DataSourse=myDataSet, DataMember=”Tablename”

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

Sidebar

Related Questions

I'm trying to write a function that does something like this: >>> Give-me the
I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write a simple recursive function that look over list and return
I am trying to write a ruby function that iterates through an array of
I'm trying to write a function that reads files from a deferred directory which
I'm trying to write an extension method that will add the function HasFactor to
I'm trying to write a function to get the Windows-equivalent of HOME. My C
I'm trying to write a small function that takes in a filepath (where the
I'm trying to write a Chrome Extension that just adds a CSS file to

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.