hi i have a datagridview showing the data from a database. Now i have a problem because i want the selected row from the datagridview to be shown on another datagridview near it on button click.

I managed to make it from datagridview to listbox but from datagridview to listbox no. can anyone help me pls thanks.

here is the add for the method to be shown on the datagridview2

public void AddtoBill(DataGridView dgv)
        {
            DataGridViewRow row = dgv.SelectedRows[0];
            int a = row.Cells[0].Value.ToString());
            int b = row.Cell[1].Value.ToString());
            if (!row.IsNewRow)
            {
                //listBox1.Items.Add(a + b);
                  datagridview2.row.add(a + b);
            }
        }

Firstly, you are implicitly converting strings to int values which should be throwing an exception: int a = row.Cells[0].Value.ToString()); You also have an extra ')' on the end that shouldn't be there.
Try something like:

int a = Convert.ToInt32(row.Cells[0].Value);

although if you cant be certain the value will be a number you should use:

int a;
bool success = int.TryParse(row.Cells[0].Value.ToString(), out a);
if(success)
{
    //process data
}
else
{
    MessageBox.Show("Cell does not contain a number");
}

Now, supposing you fix it so that the ints are correctly assigned. If a=2 and b=3, when you call

datagridview2.Rows.Add(a + b);

you will actually add 5 empty rows. If you call Rows.Add() with an int as a parameter it consider the number to be the number of rows to add.

This is all moot if you haven't added any columns to your datagridview. If there are no columns, then you wont see any rows.

So, you need to a) check you have columns in the second datagridview, b) rethink what value you want to put in the new row and c) ensure you are casting your values correctly.

Hi first of all thanks for your help. Secondly sorry for the silly mistakes lol. Now i created something like this. But there is an exception error. "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound".

I am missing a cast or something?

public void AddtoBill(DataGridView dgv)
        {
            DataGridViewRow row = dgv.SelectedRows[0];
            string a = row.Cells[0].Value.ToString();
            string b = row.Cells[1].Value.ToString();

            try
            {
                if (!row.IsNewRow)
                {
                    dgvBill.Rows.Add(a + b);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

>Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

Add data into dgvBill's DataSource. For example,

I presume that you have a myDataSet dataSource which was bound with dgvBill datagridview control.

myDataSet["yourTableName"].Rows.Add(colVal1,ColVal2,...)

hi thanks for your reply. i managed to store them on a data table. now the problem is i tried dt.NewRow() but it is writing in the same row. :/

DataGridViewRow row = dgv.SelectedRows[0];
            string a = row.Cells[0].Value.ToString();
            string b = row.Cells[1].Value.ToString();
            
            DataTable dt2 = new DataTable();
            dgvBill.DataSource = dt2;
            dt2.Columns.Add("Product");
            dt2.Columns.Add("Price");
                if (!row.IsNewRow)
                {
                    dt2.Rows.Add(a,b);
                    
                }
            dt2.NewRow();

hi just asking do i need to make the datagridview to increment or something so records will be added to new row?

Declare variable dt2 outside the method.

....

         DataTable dt1 = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();

            dt.Columns.Add("No");
            dt.Columns.Add("Name");

            dt.Rows.Add(1, "A");
            dt.Rows.Add(2, "B");
            dt.Rows.Add(3, "C");
            dt.Rows.Add(4, "D");
            dataGridView1.DataSource = dt;
            
        
            dt1.Columns.Add("No");

            dt1.Columns.Add("Name");

            dataGridView2.DataSource = dt1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            foreach (DataGridViewRow r in dataGridView1.SelectedRows)
            {
                DataGridViewRow t = (DataGridViewRow)r.Clone();

                t.Cells[0].Value = r.Cells[0].Value;

                t.Cells[1].Value = r.Cells[1].Value;

                dt1.Rows.Add(r.Cells[0].Value, r.Cells[1].Value);
            }

        }
....
commented: i was really helpful thanks :) +1

thank you very much for your help i solved the problem thanks a lot :D :D :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.