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

解决方案 »

  1.   


            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 0) //假如在第一列显示序号
                {
                    e.Value = e.RowIndex + 1;
                }
            }还可以处理cellpainting事件,画出来
      

  2.   

    http://blog.163.com/zjlovety@126/blog/static/2241862420106128264300/
      

  3.   

    我现在不是让显示序号,而是怎么在datagridview当前选中行地方插入数据
      

  4.   

    获取当前选中行行号
    循环取出所有行赋值给DataTable dt.Rows.InsertAt 插入到指定位置
    重新绑定
      

  5.   


    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);
    直接在ie上敲的,不知行不行
      

  6.   

    好像有错的
    table.Rows[i]["序号"] = i+1;
      

  7.   

    datagrideview 数据源定义成一个  List<>对象,每进行 新增和删除操作,都把 List<>重新定义一个,再赋值
      

  8.   

    DataTable dt = new DataTable();
                DataRow drr = dt.NewRow();
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    dt.Columns.Add(dataGridView1.Columns[i].Name);
                }
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    DataRow dr = dt.NewRow();
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {
                        dr[k] = dataGridView1.Rows[j].Cells[k].Value;
                    }
                    dt.Rows.Add(dr);
                }
                int h = dataGridView1.CurrentRow.Index;
                dt.Rows.InsertAt(drr, h);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[0].HeaderText = "序号";
                dataGridView1.Columns[1].HeaderText = "操作内容";
                dataGridView1.Columns[0].Width = 60;
                dataGridView1.Columns[1].Width = 295;
                for (int i = dataGridView1.CurrentRow.Index; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i + 1;
                }
                dataGridView1.Rows[h].Cells[1].Value = dataGridView2.CurrentCell.Value.ToString();我是这样做的。。本来想在datarow里添加数据,不知道怎么添加不上去,就
     dataGridView1.Rows[h].Cells[1].Value = dataGridView2.CurrentCell.Value.ToString();了。