datdgrideview的插入行和删除行 就是datagrideview里有两行序号和记录,就是删除记录时,序号还是123.。下去,不断号;插入记录是,不断号,,怎么做啊。不更新数据库。只操作datagrideview。。跪求大神 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0) //假如在第一列显示序号 { e.Value = e.RowIndex + 1; } }还可以处理cellpainting事件,画出来 http://blog.163.com/zjlovety@126/blog/static/2241862420106128264300/ 我现在不是让显示序号,而是怎么在datagridview当前选中行地方插入数据 获取当前选中行行号循环取出所有行赋值给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);直接在ie上敲的,不知行不行 好像有错的table.Rows[i]["序号"] = i+1; datagrideview 数据源定义成一个 List<>对象,每进行 新增和删除操作,都把 List<>重新定义一个,再赋值 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();了。 求助!!DataGridView中内容的更新 如何遍历一个窗体中的所有控件 求一个SQL语句 listview滚动条的自绘!? 时间转换的问题? 编辑windatagrid时,在第一列cell敲回车,根据键入的数据填写同行其他列的内容? 新手问如何实现打包..实现变成一个安装文件...求教.... 如何让新打开的窗口让其他窗口失效 TextBox动态生成问题 一直显示不出来,请赐教。 tabControl一个诡异的情况,不知道是何原因 如何使主窗体最小化时仍显示子窗体?
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0) //假如在第一列显示序号
{
e.Value = e.RowIndex + 1;
}
}还可以处理cellpainting事件,画出来
循环取出所有行赋值给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);
直接在ie上敲的,不知行不行
table.Rows[i]["序号"] = i+1;
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();了。