我在DataGridView中自定义了一个下拉框列,所以才产生了要自己实现底部新增空行的问题。
我要实现自定义下拉框内容由空变为非空时,在DataGridView底部新增空行,我发现dataGridView.Rows.Insert()和dataGridView.Rows.Add()默认都是在顶部新增行。虽然Insert支持在指定行新增,但是仍旧不支持在最后一行后面新增。
所以我的实现方法如下,我想知道还有没有其他方法可以实现DataGridView默认的新增行操作?//在自定义控件失去焦点事件中定义处理过程。
private void DataGridViewDropListEditingControl_LostFocus(Object sender, EventArgs e)
{ if (this.Text != "" && this.rowIndex == dataGridView.RowCount - 1)
{
DataGridViewRow row = new DataGridViewRow();
int i = 0, cnt = dataGridView.RowCount;
foreach (DataGridViewColumn c in dataGridView.Columns)
{
row.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
row.Cells[i].Value = dataGridView.Rows[cnt - 1].Cells[i].Value;
dataGridView.Rows[cnt - 1].Cells[i++].Value=null;
}
dataGridView.Rows.Insert(dataGridView.Rows.Count - 1, row);
}
}
我要实现自定义下拉框内容由空变为非空时,在DataGridView底部新增空行,我发现dataGridView.Rows.Insert()和dataGridView.Rows.Add()默认都是在顶部新增行。虽然Insert支持在指定行新增,但是仍旧不支持在最后一行后面新增。
所以我的实现方法如下,我想知道还有没有其他方法可以实现DataGridView默认的新增行操作?//在自定义控件失去焦点事件中定义处理过程。
private void DataGridViewDropListEditingControl_LostFocus(Object sender, EventArgs e)
{ if (this.Text != "" && this.rowIndex == dataGridView.RowCount - 1)
{
DataGridViewRow row = new DataGridViewRow();
int i = 0, cnt = dataGridView.RowCount;
foreach (DataGridViewColumn c in dataGridView.Columns)
{
row.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
row.Cells[i].Value = dataGridView.Rows[cnt - 1].Cells[i].Value;
dataGridView.Rows[cnt - 1].Cells[i++].Value=null;
}
dataGridView.Rows.Insert(dataGridView.Rows.Count - 1, row);
}
}
请版主帮忙解决下。
=======================================================
.NET快速开发整合框架(RDIFramework.NET),基于.NET的快速开发、整合框架,给用户和开发者最佳的.Net框架部署方案。
平台博客:[CNBLOGS]http://www.cnblogs.com/huyong
[CSDN]http://blog.csdn.net/chinahuyong
交流QQ:406590790 (请注明:CSDN)
平台微博:http://t.qq.com/yonghu86
邮件交流:[email protected]