所以,我正在创建一个程序来检查客户端的名称。但是我得到了这个错误。 "没有行可以添加到没有列的DataGridView控件。必须先添加列。'" 我不确定此时该做什么,因为它都是正确编码的。我只是,我不明白它为什么不起作用。我的一个朋友正在使用完全相同的代码,他的构建工作完美无瑕。
这是我的代码。
private void PopulateDataGrideView(string Name, string Status)
{
if (base.InvokeRequired)
{
base.BeginInvoke(new MethodInvoker(() => {
DataGridViewRow dataGridViewRow = new DataGridViewRow();
if (Status == "Yes")
{
dataGridViewRow.DefaultCellStyle.BackColor = Color.LightGreen;
Status = "Name Available";
}
else if (Status == "No")
{
dataGridViewRow.DefaultCellStyle.BackColor = Color.Red;
Status = "Not Available";
if (this.VisibilityBx.Checked)
{
dataGridViewRow.Visible = false;
}
}
dataGridViewRow.CreateCells(this.checkedNames, new object[] { Name, Status });
this.checkedNames.Rows.Add(dataGridViewRow);
if (scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = this.checkedNames.RowCount - 1;
}
this.progressBar1.Increment(1);
}));
}
}
private void saveAvailableNamesToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> strs = new List<string>();
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (row.Cells[1].Value.Equals("Name Available"))
{
strs.Add(row.Cells[0].Value.ToString());
}
}
if (strs.Count > 0)
{
string currentDirectory = Environment.CurrentDirectory;
DateTime now = DateTime.Now;
File.WriteAllLines(string.Concat(currentDirectory, "\\Available Names ", now.ToString("dd-MM-yyyy HH.mm.ss tt"), ".txt"), strs);
}
}
}
private void saveTakenNamesToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> strs = new List<string>();
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (row.Cells[1].Value.Equals("Not Available"))
{
strs.Add(row.Cells[0].Value.ToString());
}
}
if (strs.Count > 0)
{
string currentDirectory = Environment.CurrentDirectory;
DateTime now = DateTime.Now;
File.WriteAllLines(string.Concat(currentDirectory, "\\Taken Names ", now.ToString("dd-MM-yyyy HH.mm.ss tt"), ".txt"), strs);
}
}
}
private void scrollBx_CheckedChanged(object sender, EventArgs e)
{
if (this.checkedNames.RowCount > 0)
{
if (this.scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = this.checkedNames.RowCount - 1;
}
else if (!this.scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = 0;
}
}
}
private void VisibilityBx_CheckedChanged(object sender, EventArgs e)
{
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (!this.VisibilityBx.Checked)
{
if (!row.Visible)
{
row.Visible = true;
}
}
else if (this.VisibilityBx.Checked)
{
if ((string)row.Cells[1].Value == "Not Available")
{
row.Visible = false;
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void CheckedNames_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
答案 0 :(得分:0)
使用:
DataGridViewRow row = (DataGridViewRow)YOURDATAGRIDVIEW.Rows[0].Clone();
创建要添加的新行,而不是:
DataGridViewRow dataGridViewRow = new DataGridViewRow();
这会保留新行中的列名。
您不再需要这行代码:
dataGridViewRow.CreateCells(this.checkedNames, new object[] { Name, Status });
还要确保数据网格视图具有列,这通常是导致此错误的原因。