int rowCount = dataGridView1.Rows.Count;
时间: 2024-05-23 12:14:42 浏览: 237
This code snippet initializes an integer variable rowCount and assigns it the number of rows in a DataGridView control named dataGridView1. The variable rowCount will hold the number of rows in the DataGridView.
相关问题
当前代码会出现生成的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参数设置导入出错!!!"); } }
可以尝试在添加新行之前,先设置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参数设置导入出错!!!");
}
}
private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("A"); dt.Columns.Add("B"); dt.Columns.Add("C"); dt.Columns.Add("D"); dt.Rows.Add(new object[] { "", "", "", "" }); dt.Rows.Add(new object[] { "", "", "", "" }); dt.Rows.Add(new object[] { "", "", "", "" }); this.dataGridView1.DataSource = dt; dataGridView1.AllowUserToAddRows = false;//禁止自动生成行 dataGridView1.ColumnHeadersVisible = false;//隐藏列头 }设置一个方法,通过调用这个方法来实现dataGridView单元格合并功能
可以编写如下的方法实现 DataGridView 单元格合并:
```
private void MergeCells()
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
string columnName = dataGridView1.Columns[i].Name;
int rowCount = dataGridView1.Rows.Count;
int startIndex = 0;
int mergeCount = 1;
for (int j = 1; j < rowCount; j++)
{
DataGridViewCell prevCell = dataGridView1.Rows[j - 1].Cells[columnName];
DataGridViewCell currCell = dataGridView1.Rows[j].Cells[columnName];
if (prevCell.Value == currCell.Value)
{
mergeCount++;
}
else
{
if (mergeCount > 1)
{
Rectangle rect = dataGridView1.GetCellDisplayRectangle(i, startIndex, 1, mergeCount);
for (int k = startIndex; k < startIndex + mergeCount; k++)
{
dataGridView1.Rows[k].Cells[columnName].Visible = false;
}
dataGridView1.Controls.Add(new Label()
{
Text = prevCell.Value.ToString(),
BorderStyle = BorderStyle.None,
BackColor = Color.White,
Location = rect.Location,
Size = rect.Size
});
}
startIndex = j;
mergeCount = 1;
}
}
if (mergeCount > 1)
{
Rectangle rect = dataGridView1.GetCellDisplayRectangle(i, startIndex, 1, mergeCount);
for (int k = startIndex; k < startIndex + mergeCount; k++)
{
dataGridView1.Rows[k].Cells[columnName].Visible = false;
}
dataGridView1.Controls.Add(new Label()
{
Text = dataGridView1.Rows[startIndex].Cells[columnName].Value.ToString(),
BorderStyle = BorderStyle.None,
BackColor = Color.White,
Location = rect.Location,
Size = rect.Size
});
}
}
}
```
在代码中,首先遍历 DataGridView 的每一列,然后对于每一列中相邻且相同的单元格进行合并。如果合并了多个单元格,则在合并后的区域中心添加一个 Label 控件,以显示合并后的值。最后将合并后的单元格设置为不可见即可。
阅读全文