在datagridview中的选定行插入行 [英] Insert row at selected row in datagridview
问题描述
我正在尝试在所选行中添加一行,但它会一直将其添加到结尾。 我似乎无法弄清楚,下面是我的代码(其中一些是自定义代码)。 我试图修改最后一行'fillgridrow',但无法弄清楚如何在所选索引处插入行
。
private void button1_Click(object sender,EventArgs e)
{
DataGridViewRow dgr = dataGridView1.Rows [dataGridView1.CurrentRow.Index];
DataRow dr = itemsDT.Rows [(int)dgr.Cells [" idx"]。Value];
hiseq ++;
AddRepeatItem(dr); //更新sql查询
GetNewItem(); //选择新项目sql查询
if(newitemDT.Rows.Count == 0)
{
MessageBox.Show("写入重复项目条目时出错。调用系统。 ");
}
DataRow dr2 = newitemDT.Rows [0];
//itemsDT.Rows.Add(dr2);
itemsDT.ImportRow(dr2);
FillGridRow(dr2,newitemDT.Rows.Count - 1);
}
< blockquote>
您好,
使用
InsertAt 方法。这是一个简单的例子(我使用了BindingSource,但可以通过任何方式完成,你可以获得适当的索引)。另请注意,即使这里没有数据库交互也无关紧要。
命名空间WindowsFormsApp1
{
公共部分类Form2:表
{
private readonly BindingSource _bindingSource =
new BindingSource();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender,EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn()
{ColumnName =" id",DataType = typeof(int)});
dt.Columns.Add(new DataColumn()
{ColumnName =" FirstName",DataType = typeof(string)});
dt.Columns.Add(new DataColumn()
{ColumnName =" LastName",DataType = typeof(string)});
dt.Rows.Add(1," Mary"," Jones");
dt.Rows.Add(2,"Ane","Smith");
dt.Rows.Add(3," Jim"," Adams");
dt.Rows.Add(4,"Hank","Lebow");
_bindingSource.DataSource = dt;
dataGridView1.DataSource = _bindingSource;
}
private void button1_Click(object sender,EventArgs e)
{
if(_bindingSource.Current == null)return;
var dt =(DataTable)_bindingSource.DataSource;
var nextId = dt.AsEnumerable()
.Select(dataRow => dataRow.Field< int>(" Id"))
.Max()+1 ;
var row = dt.NewRow();
row [" id"] = nextId;
row [" FirstName"] =" Karen" ;;
row [" LastName"] =" Payne" ;;
dt.Rows.InsertAt(row,_bindingSource.Position);
}
}
}
我从Jim Adams开始,按下按钮,Karen在Jim之上,将Karen放在下面只需在该位置加+1。
Hi, I am trying to add a row at the selected row but it keeps adding it to the end. I can't seem to figure it out, below is my code (some of it is custom code). I tried to modify last line 'fillgridrow' but can't figure out how to insert row
at the index selected.
private void button1_Click(object sender, EventArgs e) { DataGridViewRow dgr = dataGridView1.Rows[dataGridView1.CurrentRow.Index]; DataRow dr = itemsDT.Rows[(int)dgr.Cells["idx"].Value]; hiseq++; AddRepeatItem(dr); //update sql query GetNewItem(); //select new item sql query if (newitemDT.Rows.Count == 0) { MessageBox.Show("Error writing repeat item entry. Call Systems. "); } DataRow dr2 = newitemDT.Rows[0]; //itemsDT.Rows.Add(dr2); itemsDT.ImportRow(dr2); FillGridRow(dr2, newitemDT.Rows.Count - 1); }
Hello,
Use InsertAt method. Here is a simple example (I used a BindingSource but is can be done any way you can get the appropriate index). Also note even though there is no database interaction here it does not matter.
namespace WindowsFormsApp1 { public partial class Form2 : Form { private readonly BindingSource _bindingSource = new BindingSource(); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn() { ColumnName = "id", DataType = typeof(int)}); dt.Columns.Add(new DataColumn() { ColumnName = "FirstName", DataType = typeof(string) }); dt.Columns.Add(new DataColumn() { ColumnName = "LastName", DataType = typeof(string) }); dt.Rows.Add(1, "Mary", "Jones"); dt.Rows.Add(2, "Ane", "Smith"); dt.Rows.Add(3, "Jim", "Adams"); dt.Rows.Add(4, "Hank", "Lebow"); _bindingSource.DataSource = dt; dataGridView1.DataSource = _bindingSource; } private void button1_Click(object sender, EventArgs e) { if (_bindingSource.Current == null) return; var dt = (DataTable) _bindingSource.DataSource; var nextId = dt.AsEnumerable() .Select(dataRow => dataRow.Field<int>("Id")) .Max() +1; var row = dt.NewRow(); row["id"] = nextId; row["FirstName"] = "Karen"; row["LastName"] = "Payne"; dt.Rows.InsertAt(row, _bindingSource.Position); } } }I start with the row with Jim Adams, press the button and Karen is above Jim, to place Karen below simply add +1 to the position.
这篇关于在datagridview中的选定行插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!