爲什麼你不只是計數的dataGridView行數,使用RowsAdded和RowsRemoved evnets? 檢查驗證碼:
public partial class Form1 : Form
{
int rowsCount;
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("col1", "Column 1");
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);
}
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
rowsCount++;
CountRows();
}
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
rowsCount--;
CountRows();
}
private void CountRows()
{
label1.Text = String.Format("Number of all rows {0}", rowsCount);
}
}
我的標籤,而綁定到網格的數據源行計數。 –