У меня есть этот простой код здесь.
Данные Grid View 1 показывает все содержимое dt (DataTable) в форме.
При нажатии button1 после того как вы изменили порядок сортировки любого столбца,
вы получите ошибку «NullReferenceException».
Когда я нажимаю button1без изменения порядка сортировки, у меня нет ошибок. Как вы это объясните?C# Forms, DataTable, DataGridView
Любая помощь приветствуется. Я потратил почти 6 часов, пытаясь решить это.
Form1.cs
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dataGridView1.DataSource = dt;
dt.Columns.Add("0", typeof(int));
dt.Columns.Add("1", typeof(int));
dt.Rows.Add(00, 01);
dt.Rows.Add(10, 11);
}
private void button1_Click(object sender, EventArgs e)
{
dt.Clear();
dt.Columns.Clear();
dt.Columns.Add("0", typeof(int));
dt.Columns.Add("1", typeof(int));
dt.Rows.Add(00, 01);
dt.Rows.Add(10, 11);
}
}
}
Form1.Designer.cs
namespace WindowsFormsApplication1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(87, 172);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(260, 207);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Вы пытались использовать ваш отладчик? –
Возможный дубликат [Что такое исключение NullReferenceException и как его исправить?] (Http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –
У меня нет опыта использования отладчика, если вы идете и открываете этот проект, вы увидите проблему. код 20 строк. Я сделал это, чтобы продемонстрировать проблему. если вы DONT меняете порядок сортировки в любом столбце, он не дает вам ошибки, если вы измените порядок сортировки, он дает null исключение. – dimitris93