DataGridViewComboBoxColumn 显示 "DataGridViewComboBoxColumn { Name=NewBox, Index=-1 }" 文本而不是 ComboBox
DataGridViewComboBoxColumn displaying "DataGridViewComboBoxColumn { Name=NewBox, Index=-1 }" text instead of ComboBox
我正在制作一个简单的 windows 表单应用程序,该应用程序将 headers 从 CSV
文件导入到 DataGridView
,然后添加一个带有组合框的行每一列。 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;
}
}
}
这是输出的屏幕截图:
这样试试:
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);
}
}
我正在制作一个简单的 windows 表单应用程序,该应用程序将 headers 从 CSV
文件导入到 DataGridView
,然后添加一个带有组合框的行每一列。 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;
}
}
}
这是输出的屏幕截图:
这样试试:
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);
}
}