[英]How to find a row exists in datagridview and append it to the last row of the datagridview
我有一個gridview將有2行,其中一些數據如下
101 111111111 1111111111009270754A094101// My first row data
9000002 1000000020222222236000000000000000000000012000000000000000000000000000000000000000//My 2nd row data
我將添加一些值,以便將其插入這兩行之間,並將已經存在的第二行移至datagrid視圖的最后一行。 像這樣,如果我添加n個值,則應該在這2個值之間插入值,並且已經存在的那一行應該移到最后一行,任何想法都請
我已使用測試代碼對此進行了編輯:
public Form1()
{
InitializeComponent();
//This can be removed before utilizing
dgv.Rows.Add("1", "1", "1");
dgv.Rows.Add("1", "1", "1");
dgv.Rows.Add("bob", "bob", "bob");
dgv.Rows.Add("1", "1", "1");
dgv.Rows.Add("1", "1", "1");
dgv.Rows.Add("1", "1", "1");
//This can be removed before utilizing
int oldrow = 2;
dgv.Rows.Add(itemArray(dgv.Rows[oldrow]));
dgv.Rows.RemoveAt(oldrow);
/*
DataGridViewRow oldRow = dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1])); dataGridView1.Rows.Remove(oldRow)
*/
}
object[] itemArray(DataGridViewRow Row)
{
int a = Row.DataGridView.ColumnCount - 1;
object[] mOut = new object[a+1];
for (int x = 0;x <= a ; x++)
{
mOut[x] = Row.Cells[x].Value;
}
return mOut;
}
對於所有其他測試,我深表歉意。
這是我寫的,但對我不起作用。 我需要的是,如果我的數組以5開頭,我想刪除gridview中已經存在的第二行,並希望在添加特定行之后附加它
if (line.StartsWith("5"))
{
int oldRow = 1;
dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1]));
dataGridView1.Rows.RemoveAt(oldRow);
dataGridView1.Rows.Add("BatchHeader", line);
m_flag = true;
StringBuilder sb = new StringBuilder();
objfileentry.createFileEntry(Append.FileName, out sb);
if (m_flag)
dataGridView1.Rows.Add("FileControl", sb.ToString());
line = string.Empty;
}
您給定的功能
private object[] itemarray(DataGridViewRow Row)
{
int a = Row.DataGridView.ColumnCount - 1;
object[] mOut = new object[a + 1];
for (int x = 0; x <= a; x++)
{
mOut[x] = Row.Cells[x].Value;
}
return mOut;
}
如果您是以這種方式綁定數據,
myGridView.DataSource = GetDataSource();
您不能以編程方式添加行。 如果沒有,您可以使用:
DataGridViewRow newRow = new DataGridViewRow();
//set row data here
myGridView.Rows.Insert(newIndex, newRow); //use Insert instead of Add/AddCopy
可以使用ListView代替datagrid嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.