日期:2014-05-18 浏览次数:21200 次
/// <summary>
/// 在最后一笔添加新行
/// </summary>
public void AddNewRow()
{
dataGridView1.Rows.Add();
int i = dataGridView1.Rows.Count - 1;
dataGridView1.CurrentCell = dataGridView1[0, i]; // 强制显示该行
dataGridView1[0, i].Selected = true; //光标移至该行
}
/// <summary>
/// 删除最后一笔记录,或者选定的记录
/// </summary>
public void DeleteRow()
{
if (dataGridView1.CurrentRow == null)
return;
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
/// <summary>
/// 在选定的记录上面插入一笔记录
/// </summary>
public void InsertRow()
{
if (dataGridView1.SelectedRows.Count >= 1)
{
int iIndex = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows.Insert(iIndex, 1);
}
}
------解决方案--------------------
是你cells超出范围不是rows
------解决方案--------------------
while (this.dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.Clear();
}