2017-12-07 32 views
0

私はdatagridviewに多くの空白行を追加しました。データをxmlに書きたいと思います。私はいくつかの小さな研究をしましたが、私が望むようにあなたがしなかったことをうれしく思っています。DataGridviewからXMLにデータを書き込む方法

DataTable dt = new DataTable(); 
dt.TableName = "SD"; 
dt = dataGridView1.DataSource as DataTable; 
dt.WriteXml("SD.xml"); 

動作していないか、実行できませんでした。

そのエラーメッセージ:

System.NullReferenceException: 'オブジェクトの インスタンスに設定されていないオブジェクト参照'

+0

どのようなエラーメッセージが表示されますか? –

+0

私はあなたのXMLファイルのパスが正しくないと思う。 –

+0

いいえ、XMLファイルパスを変更しましたが、データグリッドビューからデータをインポートできません。 –

答えて

0

したがってSystem.NullReferenceException: 'Object reference not set to an instance of an object.があなたのdataGridView1.DataSourceが実際にas DataTable

をキャストすることができることを確認してください投げることはここでは完全なスニペットです、あなたのdtが戻っnulldt = dataGridView1.DataSource as DataTable;で来ていることは非常に可能性があります。

using System; 
using System.Data; 
using System.Drawing; 
using System.Windows.Forms; 

namespace DataGridViewToXML_47696565 
{ 
    public partial class Form1 : Form 
    { 

     DataTable dgvdt = new DataTable(); 
     DataGridView dgv = new DataGridView(); 
     Button btn = new Button(); 
     public Form1() 
     { 
      InitializeComponent(); 
      InitOurStuff(); 
     } 

     private void InitOurStuff() 
     { 
      this.Controls.Add(dgv); 
      dgv.DataSource = dgvdt; 
      dgv.Dock = DockStyle.Top; 

      dgvdt.TableName = "SD"; 
      dgvdt.Columns.Add(); 
      for (int i = 0; i < 10; i++) 
      { 
       dgvdt.Rows.Add($"row {i}"); 
      } 

      this.Controls.Add(btn); 
      btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5); 
      btn.Text = "Click me"; 
      btn.Click += Btn_Click; 

     } 

     private void Btn_Click(object sender, EventArgs e) 
     { 
      dgvdt.WriteXml(@"c:\temp\mydtxml.xml"); 
     } 
    } 
} 

私のDataSourceがまだDataTableではない別のスニペットがあります。

using System; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Windows.Forms; 

namespace DataGridViewToXML_47696565 
{ 
    public partial class Form2 : Form 
    { 
     BindingList<dgventry> dgvdata = new BindingList<dgventry>(); 
     DataGridView dgv = new DataGridView(); 
     Button btn = new Button(); 


     public Form2() 
     { 
      InitializeComponent(); 
      initOurStuff(); 
     } 

     private void initOurStuff() 
     { 
      dgv.Dock = DockStyle.Top; 
      dgv.DataSource = dgvdata; 
      this.Controls.Add(dgv); 

      for (int i = 0; i < 10; i++) 
      { 
       dgvdata.Add(new dgventry { col1 = $"col1 row{i}", col2 = $"col2 row{i}", col3 = $"col3 row{i}" }); 
      } 

      this.Controls.Add(btn); 
      btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5); 
      btn.Text = "Click me"; 
      btn.Click += Btn_Click; ; 
     } 

     private void Btn_Click(object sender, EventArgs e) 
     { 


      /*The DataSource connected to the DGV is not already a DataTable, 
      so I'll traverse the DataGridView and create my own DataTable out of it*/ 
      DataTable dt = new DataTable();//create the data table 
      dt.TableName = "SD";//give it a name 
      //create the appropriate number of columns 
      for (int i = 0; i < dgv.Columns.Count; i++) 
      { 
       dt.Columns.Add(dgv.Columns[i].HeaderText); 
      } 

      //loop through each row of the DataGridView 
      foreach (DataGridViewRow currentRow in dgv.Rows) 
      { 
       dt.Rows.Add(); 
       int runningCount = 0; 
       //loop trough each column of the row 
       foreach (DataGridViewCell item in currentRow.Cells) 
       { 
        dt.Rows[dt.Rows.Count - 1][runningCount] = item.FormattedValue; 
        runningCount++; 
       } 
      } 

      if (dt != null) 
      { 
       dt.WriteXml(@"c:\temp\mynewxml.xml"); 
      } 

     } 
    } 


    public class dgventry 
    { 
     public string col1 { get; set; } 
     public string col2 { get; set; } 
     public string col3 { get; set; } 
    } 
} 

別のスニペットがあります。この場合、DataGridViewは空になります。ユーザーは手動でデータを入力する必要があります。

using System; 
using System.Data; 
using System.Drawing; 
using System.Windows.Forms; 

namespace DataGridViewToXML_47696565 
{ 
    public partial class Form3 : Form 
    { 

     DataGridView dgv = new DataGridView(); 
     Button btn = new Button(); 

     public Form3() 
     { 
      InitializeComponent(); 
      initOurStuff(); 
     } 

     private void initOurStuff() 
     { 
      dgv.Dock = DockStyle.Top; 
      this.Controls.Add(dgv); 
      for (int i = 0; i < 10; i++) 
      { 
       DataGridViewColumn newcol = new DataGridViewTextBoxColumn(); 
       newcol.Name = $"col{i}"; 
       dgv.Columns.Add(newcol); 
      } 

      this.Controls.Add(btn); 
      btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5); 
      btn.Text = "Click me"; 
      btn.Click += Btn_Click; ; 
     } 

     private void Btn_Click(object sender, EventArgs e) 
     { 
      DataTable dt = new DataTable();//create the data table 
      dt.TableName = "SD";//give it a name 
      //create the appropriate number of columns 
      for (int i = 0; i < dgv.Columns.Count; i++) 
      { 
       dt.Columns.Add(dgv.Columns[i].HeaderText); 
      } 

      //loop through each row of the DataGridView 
      foreach (DataGridViewRow currentRow in dgv.Rows) 
      { 
       dt.Rows.Add(); 
       int runningCount = 0; 
       //loop trough each column of the row 
       foreach (DataGridViewCell item in currentRow.Cells) 
       { 
        dt.Rows[dt.Rows.Count - 1][runningCount] = item.FormattedValue; 
        runningCount++; 
       } 
      } 

      if (dt != null) 
      { 
       dt.WriteXml(@"c:\temp\mynewxml.xml"); 
      } 
     } 
    } 
} 
+0

私の友人は助けてくれてありがとうございます。私はdatagridviewを既にhavaにしています。これは既に空のスペースに行を追加するだけです。これは私にとってはうまくいきませんでした。私は10列のdatagridviewを持っています。これらの空の行にデータを入力することができます。私はこのdatagridviewからデータを読み取ることができません。私はXMLファイルに書き込むdatagridviewからデータを読みたいと思いますか? –

+0

これらは、Datagridviewの作成方法、作成方法、およびXMLへのエクスポート方法を2通りの方法で示す2つのスニペットです。私はあなたの問題に対する答えがそこにあると強く信じています。 DataGridViewをどのように配置しているかを私たちに示すわけではありませんので、回答する際には一定の前提が必要です。 –

+0

@DavidHayterは、3番目のスニペットを追加しました。 –