日期:2014-05-18  浏览次数:20864 次

datdgrideview的插入行和删除行
就是datagrideview里有两行序号和记录,就是删除记录时,序号还是123.。。。。下去,不断号;
插入记录是,不断号,,怎么做啊。不更新数据库。只操作datagrideview。。
跪求大神

------解决方案--------------------
C# code

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 0) //假如在第一列显示序号
            {
                e.Value = e.RowIndex + 1;
            }
        }

------解决方案--------------------
获取当前选中行行号
循环取出所有行赋值给DataTable 

dt.Rows.InsertAt 插入到指定位置
重新绑定
------解决方案--------------------
C# code

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);