我的数据网格视图中存在问题。
datagridview来自文本文件:
学生姓名|年级|学生编号|性别
John,7,54015, 1
Jessy,3,2014年级, 2
Kyle,9,41812, 1
霍华德,6,646年级, 1
我的代码:
var lines = File.ReadAllLines(@"C:\Users\1\Desktop\1.txt");
if (lines.Count() > 0)
{
foreach (var columnName in lines.FirstOrDefault()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
dataGridView1.Columns.Add(columnName, columnName);
}
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (cellArray.Length == dataGridView1.Columns.Count)
dataGridView1.Rows.Add(cellArray);
}
foreach (var columnName in lines.First()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
if (columnName == "Gender")
{
var dgc = new DataGridViewComboBoxColumn() { Name = "hi", HeaderText = "bye" };
// dgc.Items.AddRange("Male", "Female");
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("ID");
DataColumn dc2 = new DataColumn("Name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Rows.Add(1, "Male");
dt.Rows.Add(2, "Female");
DataGridViewComboBoxColumn c1 = new DataGridViewComboBoxColumn();
c1.DataSource = dt;
c1.DisplayMember = "Name";
c1.ValueMember = "ID";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["ID"].ToString() == "1") // 1 for MALE & 2 for FEMALE
{
c1.DefaultCellStyle.NullValue = dt.Rows[i]["Name"];
}
}
dataGridView1.Columns.Add(c1);
continue;
}
}
}
我想要的是删除第3列并将其替换为第4列,并更改此值
if(dt.Rows [i] [&#34; ID&#34;]。ToString()==&#34; 1 &#34;)// 1适用于MALE&amp; ; 2为女性 在值成员ID
到文本文件中一行的第四个值。
John,7,54015, 1
Jessy,3,2014年级, 2
Kyle,9,41812, 1
霍华德,6,646年级, 1
如果值为1,则组合框的默认值为male。 而如果值为2,则组合框的默认值为女性。 有人可以修复和编辑我的代码吗?
感谢大家!
答案 0 :(得分:4)
尝试更改第一个循环以处理&#34;性别&#34;列与其他列不同。
创建一个ComboBox列并使用两个性别值填充它。
foreach (var columnName in lines.First()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
if (columnName == "Gender")
{
var dgc = new DataGridViewComboBoxColumn() { HeaderText = "Gender" };
dgc.Items.AddRange(
new KeyValuePair<string, string>("1", "Male"),
new KeyValuePair<string, string>("2", "Female"));
dgc.ValueMember = "Key";
dgc.DisplayMember = "Value";
dataGridView1.Columns.Add(dgc);
continue;
}
dataGridView1.Columns.Add(columnName, columnName);
}
然后,当您添加数据行(包括性别)时,它应该在ComboBox中正确显示,但也允许您更改值。
编辑(更新以在ComboBox中设置默认值):
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues.Split(new[] { ',' });
if (cellArray.Length == dataGridView1.Columns.Count)
{
if (String.IsNullOrEmpty(cellArray[dataGridView1.Columns.Count]))
cellArray[dataGridView1.Columns.Count] = "1";
dataGridView1.Rows.Add(cellArray);
}
}
基本上,上面只是看了最后一个&#34;列&#34;从文件中,如果它是空的,则默认为&#34;男性&#34;。
我也删除了StringSplitOptions.RemoveEmptyEntries
。当性别列为空时,该选项只会在cellArray
中返回3个元素,这也会导致cellArray.Length == dataGridView1.Columns.Count
失败。
答案 1 :(得分:1)
这有效,
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add(1, "Male");
dt.Rows.Add(2, "Female");
var lines = File.ReadAllLines(@"C:\Users\1\Desktop\1.txt");
if (lines.Count() > 0)
{
foreach (var columnName in lines.FirstOrDefault()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
if (columnName == "Gender")
{
var c1 = new DataGridViewComboBoxColumn();
c1.DataSource = dt;
c1.DisplayMember = "Name";
c1.ValueMember = "ID";
c1.HeaderText = "Gender";
dataGridView1.Columns.Add(c1);
continue;
}
dataGridView1.Columns.Add(columnName, columnName);
}
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (cellArray.Length == dataGridView1.Columns.Count)
dataGridView1.Rows.Add(cellArray);
}
}
我还想指出你正在使用
cellValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
这可能很危险,因为StringSplitOptions.RemoveEmptyEntries
条目
Hulk,,8436,1
将被跳过。
我建议您使用DataTable作为DataSource
private void Form1_Shown(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add(1, "Male");
dt.Rows.Add(2, "Female");
var table = GetDataTable(@"C:\temp\1.txt");
foreach (DataColumn column in table.Columns)
{
var columnName = column.ColumnName;
DataGridViewColumn col;
if (columnName == "Gender")
{
var c1 = new DataGridViewComboBoxColumn();
c1.DataSource = dt;
c1.DisplayMember = "Name";
c1.ValueMember = "ID";
col = c1;
}
else
{
col = new DataGridViewTextBoxColumn();
}
col.HeaderText = "Gender";
col.DataPropertyName = columnName;
dataGridView1.Columns.Add(col);
}
dataGridView1.DataSource = table;
}
该方法可能如下所示
private DataTable GetDataTable(string fileName)
{
var table = new DataTable();
var lines = File.ReadAllLines(@"C:\temp\1.txt");
if (lines.Count() > 0)
{
foreach (var columnName in lines.FirstOrDefault()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
table.Columns.Add(columnName);
}
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (cellArray.Length == table.Columns.Count)
table.Rows.Add(cellArray);
}
}
return table;
}
答案 2 :(得分:0)
做这样的事,
public partial class Form1 : Form
{
bool bIsComboBox = false;
delegate void SetComboBoxCellType(int iRowIndex);
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
var lines = File.ReadAllLines(@"C:\\1.txt");
if (lines.Count() > 0)
{
foreach (var columnName in lines.FirstOrDefault()
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
dt.Columns.Add(columnName, typeof(string));
}
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// if (cellArray.Length == dataGridView1.Columns.Count)
dt.Rows.Add(cellArray);
}
dataGridView1.DataSource = dt;
}
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
SetComboBoxCellType objChangeCellType = new SetComboBoxCellType(ChangeCellToComboBox);
if (e.ColumnIndex == this.dataGridView1.Columns["Gender"].Index)
{
this.dataGridView1.BeginInvoke(objChangeCellType, e.RowIndex);
bIsComboBox = false;
}
}
private void ChangeCellToComboBox(int iRowIndex)
{
if (bIsComboBox == false)
{
DataGridViewComboBoxCell dgComboCell = new DataGridViewComboBoxCell();
dgComboCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
dgComboCell.DataSource = dt;
dgComboCell.ValueMember = "Gender";
dgComboCell.DisplayMember = "Gender";
dataGridView1.Rows[iRowIndex].Cells[dataGridView1.CurrentCell.ColumnIndex] = dgComboCell;
bIsComboBox = true;
}
}