请问数据表的Merge方法该如何使用?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dtt;
DataTable dt;
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn comName = new DataColumn("商品名", typeof(string));
DataColumn comPrice = new DataColumn("价格", typeof(int));
DataColumn comInventory = new DataColumn("库存", typeof(int));
dt.Columns.Add("商品名", typeof(string));
dt.Columns.Add("价格", typeof(int));
dt.Columns.Add("库存", typeof(int));
dt.Rows.Add("铅笔", 1, 200);
dt.Rows.Add("文件夹", 6, 50);
dataGridView1.DataSource = dt;
}
private void button2_Click(object sender, EventArgs e)
{
DataTable dtt = new DataTable();
DataColumn comName = new DataColumn("商品名", typeof(string));
DataColumn comPrice = new DataColumn("价格", typeof(int));
DataColumn comInventory = new DataColumn("库存", typeof(int));
dtt.Columns.Add("商品名", typeof(string));
dtt.Columns.Add("价格", typeof(int));
dtt.Columns.Add("库存", typeof(int));
dtt.Rows.Add("铅笔", 3, 600);
dtt.Rows.Add("文件夹", 5, 800);
dataGridView2.DataSource = dtt;
}
private void button3_Click(object sender, EventArgs e)
{
dt.Merge(dtt); //这里出了问题
dataGridView3.DataSource = dt;
}
}
}
以上代码编译能够通过,button1_Click与button2_Click的代码都正常,button3_Click时出现异常,提示信息如下:
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 WindowsFormsApp1.Form1.button3_Click(Object sender, EventArgs e) 位置 D:\数据表操作\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:行号 62
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
请赐教