日期:2014-05-18 浏览次数:20965 次
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0) //假如在第一列显示序号 { e.Value = e.RowIndex + 1; } }
------解决方案--------------------
获取当前选中行行号
循环取出所有行赋值给DataTable
dt.Rows.InsertAt 插入到指定位置
重新绑定
------解决方案--------------------
var table = dataGridView1.DataSource as DataTable; int currentIndex = dataGridView1.CurrentCellAddress.Y; for(int i=currentIndex;i<table.Rows.Count();i++) { table[i]["序号"] = i+1; } //新加的行 var row = table.NewRow(); row["序号"] = currentIndex; row["记录"] = "记录"; table.Rows.InsertAt(row,currentIndex);