したがってSystem.NullReferenceException: 'Object reference not set to an instance of an object.
があなたのdataGridView1.DataSource
が実際にas DataTable
をキャストすることができることを確認してください投げることはここでは完全なスニペットです、あなたのdt
が戻っnull
dt = 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");
}
}
}
}
どのようなエラーメッセージが表示されますか? –
私はあなたのXMLファイルのパスが正しくないと思う。 –
いいえ、XMLファイルパスを変更しましたが、データグリッドビューからデータをインポートできません。 –