c# - DataGridView添加行运行时错误单元格的模型

时间:2017-03-30 09:32:28

标签: c# datagridview

我有一个DataGridView组件,用于向用户显示一些值。它由5列组成:4个字符串和1个复选框。这是我创建列并将它们添加到conrtol的代码:

// class variables
const int NUM_COLUMNS = 5;
DataGridViewColumn[] columns;
DataGridViewCheckBoxColumn checkColumn;
List <String> columnsHeaderName;

 // init method
 private void init_dataGridView()
    {            
        // init all components
        columnsHeaderName = new List<string>();
        columns = new DataGridViewColumn[NUM_COLUMNS - 1]; // minus one becouse last one is a check box column
        checkColumn = new DataGridViewCheckBoxColumn(); // last one
        // columns descriptions
        columnsHeaderName.Add("File path");
        columnsHeaderName.Add("Sampling");
        columnsHeaderName.Add("Start Date");
        columnsHeaderName.Add("End Date");
        columnsHeaderName.Add("Select");


        for (int i = 0; i < NUM_COLUMNS - 1; i++)
        {
            // create, configure and add n-1 columns
            columns[i] = new DataGridViewColumn();               
            columns[i].Name = Convert.ToString(i);
            columns[i].HeaderText = columnsHeaderName[i];
            columns[i].ReadOnly = true;
            this.dataGridView1.Columns.Add(columns[i]);
        }
         // create, configure and add last column
        checkColumn.Name = Convert.ToString(NUM_COLUMNS - 1);// (NUM_COLUMNS - 1).ToString();
        checkColumn.HeaderText = columnsHeaderName[NUM_COLUMNS - 1];
        checkColumn.Width = 50;
        checkColumn.ReadOnly = false;
        checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
        this.dataGridView1.Columns.Add(checkColumn);
    }

当我尝试向表中添加新行时出现问题

 private void LoadFileButton_Click(object sender, EventArgs e)
    {
        string file = "";           
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
           file = openFileDialog1.FileName;
            Console.WriteLine(file);
            dataGridView1.Rows.Add(new object[] { "value 1", "value 2", "value 3", "value 4", true });
        }                       
    }

dataGridView1.Rows.Add()方法的调用会引发异常。异常的描述是:&#34;至少有一个列没有任何单元格的模型&#34;

我真的不明白我做错了什么。希望有些人可以帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您无法添加DataGridViewColumn 如果您尝试运行以下代码:

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = "xyz";
col.HeaderText = "XYZ";
col.ReadOnly = true;
this.dataGridView1.Columns.Add(col);

它会工作,因为我们正在添加DataGridViewTextBoxColumn而不是DataGridViewColumn

所以可能的解决方法是

for (int i = 0; i < NUM_COLUMNS - 1; i++)
            {
                // create, configure and add n-1 columns
                columns[i] = new DataGridViewTextBoxColumn(); // or some other type that you want
                columns[i].Name = Convert.ToString(i);
                columns[i].HeaderText = columnsHeaderName[i];
                columns[i].ReadOnly = true;
                this.dataGridView1.Columns.Add(columns[i]);
            }

答案 1 :(得分:0)

试试这个

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;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace stackkcw
{
    public partial class Form1 : Form
    {
        public TcpClient client;
        private DataGridView datgdStock;
        private TextBox lablUpdates;
        private TextBox lablTime;
        private int countOfData;

        DataTable dt = new DataTable();

        public Form1()
        {

            InitializeComponent();
            init_dataGridView();
        }
        const int NUM_COLUMNS = 5;
        DataGridViewColumn[] columns;
        DataGridViewCheckBoxColumn checkColumn;
        List<String> columnsHeaderName;

        // init method
        private void init_dataGridView()
        {
            dt.Columns.Add("File path", typeof(string));
            dt.Columns.Add("Sampling", typeof(decimal));
            dt.Columns.Add("Start Date", typeof(DateTime));
            dt.Columns.Add("End Date", typeof(DateTime));
            dt.Columns.Add("Select", typeof(Boolean));

            dt.Rows.Add(new object[] { "c:\\", 123.45, DateTime.Parse("1/1/2017"), DateTime.Parse("1/2/2017"), true });
            dt.Rows.Add(new object[] { "c:\\", 123.46, DateTime.Parse("1/8/2017"), DateTime.Parse("1/9/2017"), false });
            dt.Rows.Add(new object[] { "c:\\", 123.47, DateTime.Parse("1/15/2017"), DateTime.Parse("1/16/2017"), true });
            dt.Rows.Add(new object[] { "c:\\", 123.48, DateTime.Parse("1/22/2017"), DateTime.Parse("1/23/2017"), false });
            dt.Rows.Add(new object[] { "c:\\", 123.49, DateTime.Parse("1/29/2017"), DateTime.Parse("1/30/2017"), true });


            dataGridView1.DataSource = dt;

        }


    }
}