How to find row in datagridview and add it to last row of datagridview

I have a gridview that will have 2 rows with some data as follows

101 111111111 1111111111009270754A094101// My first row data 9000002 1000000020222222236000000000000000000000012000000000000000000000000000000000000000//My 2nd row data 

I will add some values โ€‹โ€‹so that they can be inserted between these two rows, and the second existing row should move to the last row of the datagrid view. Similarly, if I add n number of values, the values โ€‹โ€‹should be inserted between these 2, and this line, which already exists, should be transferred to the last line of any idea, please

+4
source share
4 answers

I edited this with verified code:

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

We apologize for all the additional tests.

+1
source

This is what I wrote, but does not work for me. I need if my array starts at 5, I would like to delete the second row that already exists in gridview, and would like to add it after adding a specific row

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

This function is from you

 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; } 
+1
source

If you bind data in this way

 myGridView.DataSource = GetDataSource(); 

You cannot add lines programmatically. If not, you can use:

 DataGridViewRow newRow = new DataGridViewRow(); //set row data here myGridView.Rows.Insert(newIndex, newRow); //use Insert instead of Add/AddCopy 
0
source

Can you use ListView instead of datagrid?

0
source

All Articles