1

我正在制作一个简单的 Windows 窗体应用程序,它将CSV文件中的标题导入到 aDataGridView中,然后为每一列添加一个带有组合框的行。只是其中ComboBox有一些字符串文字,但是当我运行程序并加载时,CSV我得到了DataGridViewComboBoxColumn { Name=NewBox, Index=-1 }.

这是代码:

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;
using System.Diagnostics;

namespace DataGridTest3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;

            DataTable table = new DataTable();

            string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
            string firstline = lines[0];
            string[] headerLabels = firstline.Split(',');
            foreach (string headerWord in headerLabels)
            {
                table.Columns.Add(new DataColumn(headerWord));
            }

            int columncount = table.Columns.Count;
            DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn();
            dgv.ReadOnly = false;

            dgv.Name = "NewBox";
            dgv.Items.Add("YES");
            dgv.Items.Add("NO");

            int[] columnnums = new int[columncount];

            DataRow newRow = table.NewRow();

            table.Rows.Add(newRow);

            for (int i = 0; i < columncount; i++)
            {
                newRow[i] = dgv;
            }

            dataGridView1.DataSource = table;
        }
    }
}

这是输出的屏幕截图:

DGV 输出

4

1 回答 1

0

试试这样:

public void button1_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    textBox1.Text = openFileDialog1.FileName;
    DataTable table = new DataTable();

    string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
    string firstline = lines[0];
    string[] headerLabels = firstline.Split(',');
    foreach (string headerWord in headerLabels)
    {
        table.Columns.Add(new DataColumn(headerWord));
    }

    dataGridView1.DataSource = table;

    DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn
    {
        ReadOnly = false,
        Name = "NewBox"
    };

    dgv.DataSource = new string[] { "YES", "NO" };
    dataGridView1.Columns.Add(dgv);
}

更新:

public void button1_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    textBox1.Text = openFileDialog1.FileName;

    string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
    string firstline = lines[0];
    string[] headerLabels = firstline.Split(',');
    foreach (string headerWord in headerLabels)
    {
        DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn
        {
            ReadOnly = false,
            Name = headerWord,
            DataSource = new string[] { "YES", "NO" }
        };

        dataGridView1.Columns.Add(dgv);
    }
}
于 2020-01-07T23:09:04.900 回答