locked
Read a xml file

    Question

  • Hello guys.

    i have a xml file.

    i want read it then show the sections in the text box. please help me

    this is my xml file:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <speccydata version="1.10.248" formatversion="1.0" time="20110807T074014" localtime="20110807T121014+0330">
        <mainsection title="Summary" id="249">
            <section title="Operating System" id="254">
                <data title="">MS Windows 7 Ultimate 64-bit SP1</data>
            </section>
            <section title="CPU" id="250">
                <data title="Intel Mobile Core 2 Duo T8300  @ 2.40GHz">53 °C</data>
                <data title="Penryn 45nm Technology"></data>
            </section>
            <section title="RAM" id="255">
                <data title="">4.0GB Dual-Channel DDR2 @ 332MHz (5-5-5-15)</data>
            </section>
            <section title="Motherboard" id="253">
                <data title="Sony Corporation VAIO (N/A)">64 °C</data>
            </section>
            <section title="Graphics" id="251">
                <data title="Nvidia Defaul (1280x800@59Hz)"></data>
                <data title="256MB GeForce 8400M GT (Sony)">63 °C</data>
            </section>
            <section title="Hard Drives" id="252">
                <data title="244GB TOSHIBA TOSHIBA MK2546GSX ATA Device (SATA)">38 °C</data>
            </section>
            <section title="Optical Drives" id="248">
                <data title="MATSHITA BD-MLT UJ-220S ATA Device"></data>
            </section>
            <section title="Audio" id="257">
                <data title="High Definition Audio Device"></data>
            </section>
        </mainsection>
        <mainsection title="Peripherals" id="262">
            <section title="HID Keyboard Device">
                <data title="Device Kind">Keyboard</data>
                <data title="Device Name">HID Keyboard Device</data>
                <data title="Vendor">Unknown</data>
                <data title="Location">USB Input Device</data>
                <section title="Driver">
                    <data title="Date">6-21-2006</data>
                    <data title="Version">6.1.7601.17514</data>
                    <data title="File">C:\Windows\system32\DRIVERS\kbdhid.sys</data>
                    <data title="File">C:\Windows\system32\DRIVERS\kbdclass.sys</data>
                </section>
        </mainsection>
        <mainsection title="Network" id="256">
            <data title="You are not connected to the internet"></data>
            <section title="Computer Name">
                <data title="NetBIOS Name">SCORPION</data>
                <data title="DNS Name">Scorpion</data>
                <data title="Domain Name">Scorpion</data>
            </section>
            <section title="Remote Desktop">
                <section title="Console">
                    <data title="State">Active</data>
                    <data title="Domain">Scorpion</data>
                </section>
            </section>
            <section title="WinInet Info">
                <data title="">An internal error occurred.</data>
            </section>
            <section title="Network Shares">
                <data title="Users">C:\Users</data>
            </section>
        </mainsection>
    </speccydata>


    • Changed type Rudedog2MVP, Moderator Sunday, August 07, 2011 1:21 PM Post is a question
    • Changed type Bouki Sunday, August 07, 2011 1:54 PM
    • Edited by Bouki Sunday, August 07, 2011 1:56 PM
    Sunday, August 07, 2011 10:16 AM

Answers

  • Hi again,

    Finally I tried your code in a new console project. You're right. Actually reading XML files like this is hard and I always forget these calls sequence and serial stuffs :(( I done it but I strongly recommend search web for "Read and manipulate XML files using LINQ to XML" and learn LINQ to XML before doing these time-consuming serial reads. LINQ to SQL is more readable, reliable, understandable,...

    Ugly solution is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          XmlReaderSettings setting = new XmlReaderSettings();
          setting.IgnoreWhitespace = true;
          setting.IgnoreComments = true;
    
          XmlReader read = XmlReader.Create(@"C:\Users\Yasser\Desktop\SCORPION\SCORPION.xml", setting);
    
          if (read.ReadToFollowing("speccydata"))
            do
            {
              //SKIP TO SUMMARY
              while (read.ReadToFollowing("mainsection"))
                if (read["title"] == "Summary") break;
    
              if (read["title"] == "Summary" && read.ReadToFollowing("section"))
              {
                if (read.ReadToFollowing("data"))
                {
                  read.ReadStartElement("data");
                  Console.WriteLine(read.ReadContentAsString());
                  read.ReadEndElement();
                }
              }
              read.ReadToFollowing("mainsection");
              //SKIP TO CPU
              if (read["title"] != "CPU")
                while (read.ReadToFollowing("mainsection"))
                  if (read["title"] == "CPU") break;
    
              if (read["title"] == "CPU" && read.ReadToFollowing("section"))
                if (read.ReadToFollowing("data"))
                {
                  read.ReadStartElement("data");
                  Console.WriteLine(read.ReadContentAsString());
                  read.ReadEndElement();
                }
    
            } while (read.ReadToNextSibling("speccydata"));
          Console.ReadKey();
        }
      }
    }
    
    

    Good luck learning LINQ2SQL

    Best Regards,

    Yasser.


    LEARN HOW TO WRITE AN ASP.NET REUSABLE COMPONENT AND MORE IN A 30 MINUTES VIDEO: How to write an asp.net reusable component
    • Marked as answer by Bouki Sunday, August 07, 2011 5:44 PM
    Sunday, August 07, 2011 3:59 PM
  • Okay.  Do Over Time.

    Create a new form with a textbox, a button and richtextbox.

        private void Form1_Load(object sender, EventArgs e)
        {
          this.textBox1.Text = "XMLFile1.xml";
          this.GetData("XMLFile1.xml");
          
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          this.GetData(this.textBox1.Text);
          foreach ( DataRow row in this.table2.Rows )
          {
            richTextBox1.AppendText(Environment.NewLine);
            foreach ( var item in row.ItemArray )
            {
              richTextBox1.AppendText(item.ToString() + " : ");
            }
            richTextBox1.AppendText(Environment.NewLine);
          }
        }
    
        DataSet data;
        DataTable table0;
        DataTable table1;
        DataTable table2;
        DataTable table3;
        private void GetData(string fileName)
        {
          data = new DataSet();
          data.ReadXml(fileName);
          table0 = data.Tables[0];
          table1 = data.Tables[1];
          table2 = data.Tables[2];
          table3 = data.Tables[3];
          return;
        }

    LINQtoSQL if great for searching XML, but you are not really searching for an entry as much as you are trying to fetch a specific entry.  The above code will display the data you are looking for, and then some.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by Bouki Sunday, August 07, 2011 5:44 PM
    Sunday, August 07, 2011 5:32 PM

All replies

  • I got an error while trying to run this xml file: Invalid character in the given encoding. Line 8, position 71.
    Mitja
    Sunday, August 07, 2011 11:27 AM
  • dear aljoid i red the article. but its not help me.

    mitja: can i see your code?

    in the line8 we need section title not section content.

    Sunday, August 07, 2011 12:05 PM
  • i use this code to read the file but i just can read first section!!

    how can go to the next sections?

    string path = @"C:\Users\Mohammad\Desktop\SCORPION.xml";
    
          XmlReaderSettings setting = new XmlReaderSettings();
          setting.IgnoreWhitespace = true;
          setting.IgnoreComments = true;
    
          XmlReader read = XmlReader.Create(path, setting);
          
          if (read.ReadToDescendant("speccydata"))
            do
            {
              if (read.ReadToDescendant("mainsection") && read["title"] == "Summary")
                if (read.ReadToDescendant("section"))
                  if (read.ReadToDescendant("data"))
                    {
                      read.ReadStartElement("data");
                      textBox1.Text = read.ReadContentAsString();
                    }
              
              if (read.ReadToDescendant("mainsection") && read["title"] == "CPU")
                if (read.ReadToDescendant("section"))
                  if (read.ReadToDescendant("data"))
                  {
                    read.ReadStartElement("data");
                    textBox1.Text = read.ReadContentAsString();
                  }
    
            } while (read.ReadToNextSibling("speccydata"));
    


    Sunday, August 07, 2011 12:28 PM
  • dear aljodAv i dont understand what are you talking about!!

    my xmlreader working but just show the first section.

    i need go to the another section but i cant!

    Sunday, August 07, 2011 12:49 PM
  • They are saying that if you simply try to open the file without code to simply view it, then you get errors.  Your posted XML sample is not well formed.  It isn't valid XML.

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/1e8f86db-8a67-4c88-84c4-68dfec9bf316

    I posted a sample class that can read most any valid XML file, and display it in the Output Window.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 1:21 PM
  • i dont know why this error happen!!!!!!!!

    you can work with this xml code as txt format. create text file please.

    i use the speccy to create this file. you can see this softeware here: http://www.piriform.com/speccy/help?v=1.10.248&l=1033

    now when i run this code:

       XmlReaderSettings setting = new XmlReaderSettings();
       setting.IgnoreWhitespace = true;
       setting.IgnoreComments = true;
    
       XmlReader read = XmlReader.Create(path, setting);
       
       if (read.ReadToDescendant("speccydata"))
        do
        {
         if (read.ReadToDescendant("mainsection") && read["title"] == "Summary")
          if (read.ReadToDescendant("section"))
           if (read.ReadToDescendant("data"))
            {
             read.ReadStartElement("data");
             textBox1.Text = read.ReadContentAsString();
            }
         
         if (read.ReadToDescendant("mainsection") && read["title"] == "CPU")
          if (read.ReadToDescendant("section"))
           if (read.ReadToDescendant("data"))
           {
            read.ReadStartElement("data");
            textBox1.Text = read.ReadContentAsString();
           }
    
        } while (read.ReadToNextSibling("speccydata"));
    
    


    just show the first section. !!

    how can go to the other xml nodes?

    Sunday, August 07, 2011 1:37 PM
  • You understand how to see the error now?  Good.  Because until your XML is corrected, your code will not work properly.  Like I said, your XML is not well formed.  It is not valid XML, so your code will get lost trying to read it.

    Rudy    =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 1:45 PM
  • here you can dl the xml file!!!!!!!!

    http://www.mediafire.com/?xekzez5krmgdlq9

    now answer my question
    Sunday, August 07, 2011 1:53 PM
  • I do not know what "dl the xml" means.  download?

    I have already posted a link to a solution.  Your code is structurally flawed, which is why you  are having problems with it.  My class can be altered to fire an event when a desired node name is reached.  A custom EventArgs object could be used to pass the data within the node to event subscribers.

    Rudy   =8^D

     


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 2:02 PM
  • I found the flaw in your posted XML by opening it in Visual Studio.  It is missing a closing </section> tag between lines 36 and 37.  I am posting the corrected file below with a comment where I added the missing tag on what is now line 37

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
     <speccydata version="1.10.248" formatversion="1.0" time="20110807T074014" localtime="20110807T121014+0330">
       <mainsection title="Summary" id="249">
         <section title="Operating System" id="254">
           <data title="">MS Windows 7 Ultimate 64-bit SP1</data>
         </section>
         <section title="CPU" id="250">
           <data title="Intel Mobile Core 2 Duo T8300 @ 2.40GHz">53 °C</data>
           <data title="Penryn 45nm Technology"></data>
         </section>
         <section title="RAM" id="255">
           <data title="">4.0GB Dual-Channel DDR2 @ 332MHz (5-5-5-15)</data>
         </section>
         <section title="Motherboard" id="253">
           <data title="Sony Corporation VAIO (N/A)">64 °C</data>
         </section>
         <section title="Graphics" id="251">
           <data title="Nvidia Defaul (1280x800@59Hz)"></data>
           <data title="256MB GeForce 8400M GT (Sony)">63 °C</data>
         </section>
         <section title="Hard Drives" id="252">
           <data title="244GB TOSHIBA TOSHIBA MK2546GSX ATA Device (SATA)">38 °C</data>
         </section>
         <section title="Optical Drives" id="248">
           <data title="MATSHITA BD-MLT UJ-220S ATA Device"></data>
         </section>
         <section title="Audio" id="257">
           <data title="High Definition Audio Device"></data>
         </section>
       </mainsection>
       <mainsection title="Peripherals" id="262">
         <section title="HID Keyboard Device">
           <data title="Device Kind">Keyboard</data>
           <data title="Device Name">HID Keyboard Device</data>
           <data title="Vendor">Unknown</data>
           <data title="Location">USB Input Device</data>
         </section> <!-- this /section tag is missing -->
         <section title="Driver">
             <data title="Date">6-21-2006</data>
             <data title="Version">6.1.7601.17514</data>
             <data title="File">C:\Windows\system32\DRIVERS\kbdhid.sys</data>
             <data title="File">C:\Windows\system32\DRIVERS\kbdclass.sys</data>
           </section> 
      </mainsection>
       <mainsection title="Network" id="256">
         <data title="You are not connected to the internet"></data>
         <section title="Computer Name">
           <data title="NetBIOS Name">SCORPION</data>
           <data title="DNS Name">Scorpion</data>
           <data title="Domain Name">Scorpion</data>
         </section>
         <section title="Remote Desktop">
           <section title="Console">
             <data title="State">Active</data>
             <data title="Domain">Scorpion</data>
           </section>
         </section>
         <section title="WinInet Info">
           <data title="">An internal error occurred.</data>
         </section>
         <section title="Network Shares">
           <data title="Users">C:\Users</data>
         </section>
       </mainsection>
     </speccydata>
     
    

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 2:21 PM
  •     } while (read.ReadToNextSibling("speccydata"));

    Welcome to MSDN Forums.

    I've got your file and there is only one 'speccydata' in it so the C# behavior is right. Maybe you're trying this:

    } while (read.ReadToNextSibling("manisection"));
    

    Best Regards,

    Yasser.


    LEARN HOW TO WRITE AN ASP.NET REUSABLE COMPONENT AND MORE IN A 30 MINUTES VIDEO: How to write an asp.net reusable component
    Sunday, August 07, 2011 2:32 PM
  • My VS still notifies me about an error: Invalid character in the given encoding. Line 8, position 65.
    Mitja
    Sunday, August 07, 2011 2:35 PM
  • Oooopps!!!!!!!!!!

    guys let me ask my question again.

    how can read this xml file ?  and show the fields in the text box.

    this is my xml file: http://www.mediafire.com/?xekzez5krmgdlq9

    i just want read the first mainsectin.

    Sunday, August 07, 2011 2:43 PM
  • One more thing! if you want to jump from "Summary" main section to "CPU" main section and skip "Operating System" main section, you need ti rewrite second 'if' to 'while' i.e.
    XmlReaderSettings setting = new XmlReaderSettings();
      setting.IgnoreWhitespace = true;
      setting.IgnoreComments = true;
    
      XmlReader read = XmlReader.Create(path, setting);
      
      if (read.ReadToDescendant("speccydata"))
      do
      {
       if (read.ReadToDescendant("mainsection") && read["title"] == "Summary")
       if (read.ReadToDescendant("section"))
        if (read.ReadToDescendant("data"))
        {
         read.ReadStartElement("data");
         textBox1.Text = read.ReadContentAsString();
        }
    
       while (read.ReadToDescendant("mainsection") && read["title"] != "CPU") /*JUST SKIP*/;
    
       if (read.ReadToDescendant("section"))
        if (read.ReadToDescendant("data"))
        {
        read.ReadStartElement("data");
        textBox1.Text = read.ReadContentAsString();
        }
    
      } while (read.ReadToNextSibling("speccydata"));
    
    
    


    LEARN HOW TO WRITE AN ASP.NET REUSABLE COMPONENT AND MORE IN A 30 MINUTES VIDEO: How to write an asp.net reusable component
    Sunday, August 07, 2011 2:43 PM
  • I ran your code on the XML that I posted, and got this result in the textbox.  It read all of the node data.

     

    MS Windows 7 Ultimate 64-bit SP1

    That is the entire content of the first section.  Your attributes are being ignored.  The link I posted shows how to read the node attributes.  Data should not be stored in attributes.  Attributes should describe the data, and not be the data.


    Rudy    =8^D

     


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Sunday, August 07, 2011 2:50 PM
  • Sorry, there are some mistakes in my previous code. could you please see if this one addresses your need?

    XmlReaderSettings setting = new XmlReaderSettings();
     setting.IgnoreWhitespace = true;
     setting.IgnoreComments = true;
    
     XmlReader read = XmlReader.Create(path, setting);
     
     if (read.ReadToDescendant("speccydata"))
     do
     {
      //SKIP TO SUMMARY
      while (read.ReadToDescendant("mainsection"))
       if(read["title"] == "Summary") break;
    
      if (read["title"] == "Summary" && read.ReadToDescendant("section"))
      if (read.ReadToDescendant("data"))
      {
       read.ReadStartElement("data");
       textBox1.Text = read.ReadContentAsString();
      }
    
      //SKIP TO CPU
      while (read.ReadToDescendant("mainsection"))
       if(read["title"] == "CPU") break;
    
      if (read["title"] == "CPU" && read.ReadToDescendant("section"))
      if (read.ReadToDescendant("data"))
      {
      read.ReadStartElement("data");
      textBox1.Text = read.ReadContentAsString();
      }
    
     } while (read.ReadToNextSibling("speccydata"));
    
    
    

    This is my last shot ;)


    LEARN HOW TO WRITE AN ASP.NET REUSABLE COMPONENT AND MORE IN A 30 MINUTES VIDEO: How to write an asp.net reusable component
    Sunday, August 07, 2011 2:56 PM
  • Thanks for your attention.

    but same problem. with this code we can read just first section: MS Windows 7 Ultimate 64-bit SP1

    i want skip to cpu as you comment in the code!

    Sunday, August 07, 2011 3:12 PM
  • I ran your code on the XML that I posted, and got this result in the textbox.  It read all of the node data.

     

    MS Windows 7 Ultimate 64-bit SP1

    That is the entire content of the first section.  Your attributes are being ignored.  The link I posted shows how to read the node attributes.  Data should not be stored in attributes.  Attributes should describe the data, and not be the data.

     


    its true.

    now i want go to the next section. just it!

    Sunday, August 07, 2011 3:29 PM
  • I don't know what "just it" means.  Sorry.  Your target is moving.  I thought you wanted just the first section.  Now you want the next section.  Does that mean that you will eventually want all of the sections?  The code that you have written move to a specific spot in this one file.  To move to another spot, you will need to code to go to a new spot. 

    As I said, your code is structurally flawed.  Modifying it to something different is prohibitive.  You would need to write another method that re-reads the file to find something new.  Your approach almost requires a separate method to read each element.  Your code opens the file and reads a specific element.  It is not flexible.

    The code that I have written can move to and through any spot in any file.  The method calls to Indent and Unindent are used to identify the beginning and ending of a node.  The node name is contained in the reader.Name property.  The node value is contained in the reader.Value property.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 3:58 PM
  • Hi again,

    Finally I tried your code in a new console project. You're right. Actually reading XML files like this is hard and I always forget these calls sequence and serial stuffs :(( I done it but I strongly recommend search web for "Read and manipulate XML files using LINQ to XML" and learn LINQ to XML before doing these time-consuming serial reads. LINQ to SQL is more readable, reliable, understandable,...

    Ugly solution is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          XmlReaderSettings setting = new XmlReaderSettings();
          setting.IgnoreWhitespace = true;
          setting.IgnoreComments = true;
    
          XmlReader read = XmlReader.Create(@"C:\Users\Yasser\Desktop\SCORPION\SCORPION.xml", setting);
    
          if (read.ReadToFollowing("speccydata"))
            do
            {
              //SKIP TO SUMMARY
              while (read.ReadToFollowing("mainsection"))
                if (read["title"] == "Summary") break;
    
              if (read["title"] == "Summary" && read.ReadToFollowing("section"))
              {
                if (read.ReadToFollowing("data"))
                {
                  read.ReadStartElement("data");
                  Console.WriteLine(read.ReadContentAsString());
                  read.ReadEndElement();
                }
              }
              read.ReadToFollowing("mainsection");
              //SKIP TO CPU
              if (read["title"] != "CPU")
                while (read.ReadToFollowing("mainsection"))
                  if (read["title"] == "CPU") break;
    
              if (read["title"] == "CPU" && read.ReadToFollowing("section"))
                if (read.ReadToFollowing("data"))
                {
                  read.ReadStartElement("data");
                  Console.WriteLine(read.ReadContentAsString());
                  read.ReadEndElement();
                }
    
            } while (read.ReadToNextSibling("speccydata"));
          Console.ReadKey();
        }
      }
    }
    
    

    Good luck learning LINQ2SQL

    Best Regards,

    Yasser.


    LEARN HOW TO WRITE AN ASP.NET REUSABLE COMPONENT AND MORE IN A 30 MINUTES VIDEO: How to write an asp.net reusable component
    • Marked as answer by Bouki Sunday, August 07, 2011 5:44 PM
    Sunday, August 07, 2011 3:59 PM
  • I don't know what "just it" means.  Sorry.  Your target is moving.  I thought you wanted just the first section.  Now you want the next section.  Does that mean that you will eventually want all of the sections?  The code that you have written move to a specific spot in this one file.  To move to another spot, you will need to code to go to a new spot. 

    As I said, your code is structurally flawed.  Modifying it to something different is prohibitive.  You would need to write another method that re-reads the file to find something new.  Your approach almost requires a separate method to read each element.  Your code opens the file and reads a specific element.  It is not flexible.

    The code that I have written can move to and through any spot in any file.  The method calls to Indent and Unindent are used to identify the beginning and ending of a node.  The node name is contained in the reader.Name property.  The node value is contained in the reader.Value property.


    Hello.

    Thanks a lot for your attention.

    i want read part of this xml code. first mainsection and network driver.

    forgot my code. please help me.

    Sunday, August 07, 2011 5:07 PM
  • Okay.  Do Over Time.

    Create a new form with a textbox, a button and richtextbox.

        private void Form1_Load(object sender, EventArgs e)
        {
          this.textBox1.Text = "XMLFile1.xml";
          this.GetData("XMLFile1.xml");
          
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          this.GetData(this.textBox1.Text);
          foreach ( DataRow row in this.table2.Rows )
          {
            richTextBox1.AppendText(Environment.NewLine);
            foreach ( var item in row.ItemArray )
            {
              richTextBox1.AppendText(item.ToString() + " : ");
            }
            richTextBox1.AppendText(Environment.NewLine);
          }
        }
    
        DataSet data;
        DataTable table0;
        DataTable table1;
        DataTable table2;
        DataTable table3;
        private void GetData(string fileName)
        {
          data = new DataSet();
          data.ReadXml(fileName);
          table0 = data.Tables[0];
          table1 = data.Tables[1];
          table2 = data.Tables[2];
          table3 = data.Tables[3];
          return;
        }

    LINQtoSQL if great for searching XML, but you are not really searching for an entry as much as you are trying to fetch a specific entry.  The above code will display the data you are looking for, and then some.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by Bouki Sunday, August 07, 2011 5:44 PM
    Sunday, August 07, 2011 5:32 PM
  • Use this class to display the data tables.  There are four of them created from your xml file.

    Add the following code as a CLASS.  Visual Studio will recognize it as a form.  Do not create a new form and this code to it.

      public partial class DataTableForm : Form
      {
    
        #region Do not modify code in this region
    
        /// <summary>
    
        /// Required designer variable.
    
        /// </summary>
    
        private System.ComponentModel.IContainer components = null;
    
    
    
        /// <summary>
    
        /// Clean up any resources being used.
    
        /// </summary>
    
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    
        protected override void Dispose(bool disposing)
        {
    
          if ( disposing && (components != null) )
          {
    
            components.Dispose();
    
          }
    
          base.Dispose(disposing);
    
        }
    
    
    
        #region Windows Form Designer generated code
    
    
    
        /// <summary>
    
        /// Required method for Designer support - do not modify
    
        /// the contents of this method with the code editor.
    
        /// </summary>
    
        private void InitializeComponent()
        {
    
          this.dataGridView1 = new System.Windows.Forms.DataGridView();
    
          ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    
          this.SuspendLayout();
    
          // 
    
          // dataGridView1
    
          // 
    
          this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    
          this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
    
          this.dataGridView1.Location = new System.Drawing.Point(0, 0);
    
          this.dataGridView1.Name = "dataGridView1";
    
          this.dataGridView1.Size = new System.Drawing.Size(292, 266);
    
          this.dataGridView1.TabIndex = 0;
    
          
          // 
    
          // DataTableForm
    
          // 
    
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    
          this.ClientSize = new System.Drawing.Size(292, 266);
    
          this.Controls.Add(this.dataGridView1);
    
          this.Name = "DataTableForm";
    
          this.Text = "DataTableForm";
    
          this.Load += new System.EventHandler(this.DataTableForm_Load);
    
          ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    
          this.ResumeLayout(false);
    
    
    
        }
    
    
    
        #endregion
    
    
    
        private System.Windows.Forms.DataGridView dataGridView1;
    
        #endregion
    
        DataTable dataTable;
    
        BindingSource bindingSource;
    
        public DataTableForm()
        {
    
          InitializeComponent();
    
          this.Load += new EventHandler(DataTableForm_Load);
    
        }
    
        public DataTableForm(DataTable table)
    
          : this()
        {
    
          this.dataTable = table;
    
    
    
        }
    
    
    
    
    
        private void DataTableForm_Load(object sender, EventArgs e)
        {
    
          this.bindingSource = new BindingSource();
    
          this.bindingSource.DataSource = this.dataTable;
    
          this.dataGridView1.DataSource = this.bindingSource;
    
          dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
    
          return;
    
        }
    
      }

     


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Sunday, August 07, 2011 5:44 PM
  • can you upload the solution for me? i m using VS 2008.

    i cant understand what you do!

    im really confused. i think this shoud be a simple .


    Sunday, August 07, 2011 6:47 PM
  • All of the code that has been posted so far should work with VS2008. 

    Describe the errors and problems that you are experiencing.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Monday, August 08, 2011 3:57 PM