2016-09-26 8 views
0

私は7列のdatagridviewを持っています。このdatagridviewには、データセットが接続されていません。ユーザーは、dataGridView.Rows.Addコマンドを使用して値を入力します。データセットなしでLinqを使用してDataGridViewをXMLに保存する方法

これらのファイルをXML(Linq)に保存する可能性はありますか?

+0

これにはいくつかの方法があるかもしれません。あなたがデータセットを使用していない理由は何ですか?私はどのようにデータベースに接続しようとしているのですか?あなたはEFのような他のテクノロジーを使うことができますが、本当に達成したいことに本当に依存しています。これについてもう少し詳しく説明できますか? – Lance

+0

データベースへの接続がないのは、列を含むグリッドだけです。私はそれとこの作業を実行するための適切なコンポーネントを取得しません。 –

+0

WPFまたはwinformsを使用していますか? WPFでは、DataGridを常にobservablecollectionにバインドしてシリアル化できます。 http://stackoverflow.com/questions/8633398/xmlserialize-an-observablecollection – Lance

答えて

1

私は

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     public Form1() 
     { 
      InitializeComponent(); 

      DataTable dt = new DataTable(); 
      dt.Columns.Add("Name", typeof(string)); 
      dt.Columns.Add("Age", typeof(int)); 

      dt.Rows.Add(new object[] { "John", 25 }); 
      dt.Rows.Add(new object[] { "Mary", 26 }); 
      dt.Rows.Add(new object[] { "Bill", 27 }); 
      dt.Rows.Add(new object[] { "Beth", 28 }); 

      dataGridView1.DataSource = dt; 

      //reverse 
      DataTable dt2 = new DataTable("NewTable"); 
      foreach (DataGridViewColumn column in dataGridView1.Columns) 
      { 
       dt2.Columns.Add(column.Name, column.ValueType); 
      } 
      //don't save last row of dattagridview which is the blank editable row 
      for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
      { 
       DataGridViewRow row = dataGridView1.Rows[i]; 
       DataRow newRow = dt2.Rows.Add(); 
       for (int j = 0; j < row.Cells.Count; j++) 
       { 
        newRow[j] = row.Cells[j].Value; 
       } 
      } 

      dt2.WriteXml(FILENAME, XmlWriteMode.WriteSchema); 
     } 
    } 
} 

データテーブルからのDataGridViewを作成しました。その後、DGVからデータテーブルを作成してファイルに保存しました。

+0

ありがとう、助けてください –

0

別ソリューション:

void SaveData() { 
     XDocument xmlDocument = new XDocument(new XElement("Grid")); 

     foreach(DataGridViewRow row in dataGridView1.Rows) 
     xmlDocument.Root.Add(
      new XElement("Grid", 
       new XAttribute("xxx1", row.Cells[0].Value.ToString()), 
       new XAttribute("xxx2", row.Cells[1].Value.ToString()), 
       new XAttribute("xxx3", row.Cells[2].Value.ToString()), 
       new XAttribute("xxx4", row.Cells[3].Value.ToString()), 
       new XAttribute("xxx5", row.Cells[4].Value.ToString()), 
       new XAttribute("xxx6", row.Cells[5].Value.ToString()), 
       new XAttribute("xxx7", row.Cells[6].Value.ToString()))); 
     xmlDocument.Save("@Path"); 
    } 

    void LoadData() { 
     try { 
      XDocument xmlDocument = XDocument.Load("@Path"); 

      foreach(XElement el in xmlDocument.Root.Elements()) { 
       switch(el.Name.LocalName) { 
        case "Grid": 
         dataGridView1.Rows.Add(el.Attribute("xxx1").Value,el.Attribute("xxx2").Value, 
          el.Attribute("xxx3").Value,el.Attribute("xxx4").Value,el.Attribute("xxx5").Value, 
          el.Attribute("xxx6").Value,el.Attribute("xxx7").Value); 
         break; 
       } 
      } 
     } catch(Exception ex) { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
関連する問題