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

  • Home
  • SEARCH
  • 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 8586923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:26:12+00:00 2026-06-11T22:26:12+00:00

I am a beginner in c#, I created one dataGridView1 in a Form to

  • 0

I am a beginner in c#, I created one dataGridView1 in a Form to which I added some rows and columns (without using DataSet and Datatable). Now I need to send the data of dataGridView1 to an xml file. The xml file should update whenever I click on that button. I need to do this without using Datatable (or tables). I tried the below code (not working)

OnButtonClick

        XmlTextWriter newXml = new XmlTextWriter("d:/newXML.xml", Encoding.UTF8);
        DataSet ds = new DataSet(dataGridView1.Rows.ToString()); /* May be I am missing something here */
        ds.WriteXml(newXml);

xml file is created successfully but showing <System.Windows.Forms.DataGridViewRowCollection /> in it.

If I add BindingSource bs = (BindingSource)dataGridView1.DataSource; before DataSet, it is showing error “Object reference not set to an instance of an object.”

Note: If the xml file is not their then it should create one.

Thanks in advance.

  • 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-11T22:26:14+00:00Added an answer on June 11, 2026 at 10:26 pm

    You can instantiate a DataTable and populate it from your dataGridView1 control:

            DataTable table = new DataTable("Customers");
            // copy the correct structure from datagridview to the table
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                table.Columns.Add(column.Name, typeof(string));
            }
    
            // populate the datatable from datagridview
            for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
            {
                table.Rows.Add();
                for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
                {
                    table.Rows[rowIndex][columnIndex] = dataGridView1[rowIndex, columnIndex].Value;
                }
            }
    

    then continue with the rest of your code:

        DataSet ds = new DataSet();
        ds.Tables.Add(table);
        ds.WriteXml("d:/newXML.xml", System.Data.XmlWriteMode.IgnoreSchema);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a LaTeX beginner. Up until now whenever I've created a table (using the
I'm a beginner at using ActiveMQ with C#. I've created a simple windows form
I am a beginner programmer, using python on Mac. I created a function as
I have created a spinner with 3 options: Beginner, Advanced and Pro. When one
I'm a beginner with C++ and now have a problem with one of my
I'm a beginner at Android Development. I've created a game. One Bitmap (1) is
I'm a beginner i have some problem with NSArray. I created an array and
I have created a MySQL table where one of the columns stores a BLOB
I'm a very beginner in html/css. I have created a one level dropdown menu
I am a beginner in using autotool, I want to add some shell script

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.