Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a windows form contains datagridview (having student information) and a menu(having items(save,open)
i want that

after filling all student information

--> if user click save button
save as window will open
-->then user entered the name of file and file will save (extension of file could be anything ..for example .sgc)
---> then all the data of datagridview that entered by user will save on that file
same as a want to open that file..

please help..!!!
Here is code..


C#
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
 {
     string folderPath = string.Empty;

     SaveFileDialog savefiledialog1 = new SaveFileDialog();
     savefiledialog1.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
     savefiledialog1.RestoreDirectory = true;

     if (savefiledialog1.ShowDialog() == DialogResult.OK)
     {
         folderPath = savefiledialog1.FileName;


                 if (!System.IO.File.Exists(folderPath))
                 {
                     XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                     XmlComment comment = doc.CreateComment("This is an XML Generated File");
                     XmlElement root = doc.CreateElement("calcs");
                     XmlElement calc = doc.CreateElement("calc");
                     XmlAttribute Name = doc.CreateAttribute("Name");
                     //Add the values for each nodes
                     Name.Value = textBox5.Text;


                     //Construct the document
                     doc.AppendChild(declaration);
                     doc.AppendChild(comment);
                     doc.AppendChild(root);
                     root.AppendChild(calc);
                     calc.Attributes.Append(Name);

                     doc.Save(savefiledialog1.FileName);

                 }


                 //Show confirmation message
                 MessageBox.Show("Details have been added to the XML File.");

                 //Reset text fields for new input
                 //textBox5.Text = String.Empty;
             }
         }

 private void openSignalChainToolStripMenuItem_Click(object sender, EventArgs e)
 {
     XmlDataDocument xmldoc = new XmlDataDocument();
     OpenFileDialog openfiledialog1 = new OpenFileDialog();
   //  openfiledialog1.InitialDirectory = "c:\\";
     openfiledialog1.FileName = "Document";
     openfiledialog1.DefaultExt = ".xml";
     openfiledialog1.Filter = "xml file (.xml)|*.xml";
    openfiledialog1.FilterIndex = 2;
     Nullable<bool> result = Convert.ToBoolean(openfiledialog1.ShowDialog());
     openfiledialog1.RestoreDirectory = true;
     if (result==true)
     {
             string filename = openfiledialog1.FileName;
             try
             {
                 XmlNodeList xmlnode;
                 int i = 0;
                 string str = null;
                 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                 xmldoc.Load(fs);
                 xmlnode = xmldoc.GetElementsByTagName("Product");
                 for (i = 0; i <= xmlnode.Count - 1; i++)
                 {
                     xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                     str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                     MessageBox.Show(str);
                     textBox5.Text = str;
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("File can not found!!!!");
             }


     }
 }
Posted
Updated 9-Jul-14 10:38am
v5
Comments
[no name] 4-Jul-14 3:58am    
And you expect us to do what with this?
TusharZoo 4-Jul-14 4:08am    
sir.. how do i make a file that save all the values entered by users in datagridview ..
[no name] 4-Jul-14 4:12am    
What to you mean "how"? Do some research and write some code is how. I can't believe that you could not find this information. Did you make any effort at all? Start with http://msdn.microsoft.com/en-us/library/system.io.file.aspx
TusharZoo 4-Jul-14 4:27am    
sir i have searched many times ...if you have any solution please give me ..i really need it..

thank you
[no name] 4-Jul-14 4:38am    
No sorry. I don't believe that you have searched at all. If you had, you would have found the information you was looking for and been able to do your homework assignment yourself.

1 solution

If you use DataSet as datasorce of yours grid just use WriteXml to save data and ReadXml to load data back to grid.

http://msdn.microsoft.com/en-us/library/System.Data.DataSet%28v=vs.110%29.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900