views:

134

answers:

4

Hi all, I am having 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 such that it should be inserted between these 2 rows and that 2nd row already exists should be move to the last row of the datagrid view. Like that if i add n number of values the values should be inserted in between those 2 and that row which was already exists should be moved to the last row any idea please

+1  A: 

I have editted this with tested 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;

        }

I apologize for all of the additional testing.

Michael Eakins
What it meant by item array
Dorababu
But i am not getting any property like that
Dorababu
I am getting an error as Error 2 Cannot implicitly convert type 'int' to 'System.Windows.Forms.DataGridViewRow'
Dorababu
Check to ensure that this row is actually a row and not an integer, please. DataGridViewRow oldRow = dgv.Rows.Add(itemArray(dgv.Rows[2]));
Michael Eakins
I write this DataGridViewRow oldRow = dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1])); dataGridView1.Rows.Remove(oldRow); But i am getting that error
Dorababu
Same error again
Dorababu
I changed it to int instead of datagridview row but in the function item array i am getting an error at mOut[x] = Row.Cells[x].Value as Index was outside the bounds of the array.
Dorababu
But i am unable to delete that row still that row is visible
Dorababu
I have posted new code, which I tested to verify.
Michael Eakins
Even then i am unable to remove that row
Dorababu
If you are still unable to remove the row your row is likely databound, you will need to use a currency Manager to unbind the data in your datagridview. http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.aspx
Michael Eakins
+1  A: 

This was what i written but does not work for me. What i need is if my array starts with 5 i would like to remove the 2nd row which was already exists in gridview and would like to append it after the particular row added

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

The given function by 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;

    }
Dorababu
What is your datasource for your datagridviewrow?
Michael Eakins
I am not using any datasource if i fill some values on a form i will write it to grid by making the values as an array of strings
Dorababu
A: 

If you are binding data in this way,

myGridView.DataSource = GetDataSource();

You can't add rows 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
Danny Chen
A: 

Can you use ListView instead of datagrid?

Daniel Mošmondor