当前代码会出现生成的datagreviw多出一行空白行,帮我改进一下解决这个问题: private void DataImport() //更新datagridview1 { try { lock (this) { if (dataGridView1.Rows.Count != 0) { dataGridView1.Rows.Clear(); } } for (int i = 0; i < Controller.StatusTable_Parameter.Count; i++) { if (Controller.StatusTable_Parameter[i][2].Substring(Controller.StatusTable_Parameter[i][2].IndexOf('=') + 1).Replace(" ", "\n\r") == "0") continue; dataGridView1.Rows.Add(); dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value = Controller.StatusTable_Parameter[i][1].Substring(Controller.StatusTable_Parameter[i][1].IndexOf('=') + 1).Replace(" ", "\n\r"); for (int j = 2; j < Controller.StatusTable_Parameter[i].Count; j++) { dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[j - 1].Value = Controller.StatusTable_Parameter[i][j].Substring(Controller.StatusTable_Parameter[i][j].IndexOf('=') + 1).Replace(" ", "\n\r"); } } } catch { Controller.MessageDisplay("ini参数设置导入出错!!!"); } }
时间: 2024-02-15 17:28:19 浏览: 26
可以尝试在添加新行之前,先设置DataGridView的RowTemplate,这样可以避免添加空白行。修改后的代码如下:
private void DataImport() //更新datagridview1
{
try
{
lock (this)
{
if (dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.Clear();
}
}
// 添加RowTemplate
dataGridView1.RowTemplate = new DataGridViewRow();
for (int i = 0; i < Controller.StatusTable_Parameter.Count; i++)
{
if (Controller.StatusTable_Parameter[i][2].Substring(Controller.StatusTable_Parameter[i][2].IndexOf('=') + 1).Replace(" ", "\n\r") == "0")
continue;
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value = Controller.StatusTable_Parameter[i][1].Substring(Controller.StatusTable_Parameter[i][1].IndexOf('=') + 1).Replace(" ", "\n\r");
for (int j = 2; j < Controller.StatusTable_Parameter[i].Count; j++)
{
dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[j - 1].Value = Controller.StatusTable_Parameter[i][j].Substring(Controller.StatusTable_Parameter[i][j].IndexOf('=') + 1).Replace(" ", "\n\r");
}
}
}
catch
{
Controller.MessageDisplay("ini参数设置导入出错!!!");
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)