我正在制作一个简单的 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;
}
}
}
这是输出的屏幕截图: