Hello.
I have a datagrid with 9 columns. I want to add a one row from data table who is that same. I believs I can do it that:

DataGridViewRow drow = new DataGridViewRow();
                        foreach (DataRow wiersz in dTable2.Rows)
                            {
                                for (int i = 0; i <= ilosckolumn - 1; i++)
                                {
                                    drow.Cells[i].Value = wiersz[i].ToString();
                                }
                            }

But its not working.
Any sugestions?

datagridview1.Rows.Add(); //add new row to dgv control
int rowIndex = datagridview1.Rows.Count - 1; //get this row`s index

//now insert one row from dataTable:
//I will show you how to add 1st row from dataTable:
int rowIndex = dataGridView1.Rows.Count - 1;
for (int i = 0; i < table.Rows.Count; i++) //loop through thr rows of dataTable
{
    if (i == 0) //only add row at index 0 (1st row in dataTable)
    {
        for (int j = 0; j < table.Columns.Count; j++) //loop through thr columns of dataTable
        {
             dataGridView1[j, rowIndex].Value = table.Rows[i][j];
        }
        break; //when done, code will leave for loop (of rows) - makes code faster!
    }
}

Hope this helps.

Thank You a lot for replay!
I did was You wrote but Its add me a only last row from datatable. So I little change it:

foreach (DataRow wiersz in dTable2.Rows)
                            {
                                int rowIndex = dataGridView1.Rows.Add();
                                for (int j = 0; j < dTable2.Columns.Count; j++)
                                    {
                                        dataGridView1[j, rowIndex].Value = wiersz[j];
                                    }
                             }
                        zmianam

And its doing that same. I think I made something wrong with index of rows so he only update the last. Its true?

LOL ...
My code works 100%, and it instets 1 row from dataTable to the last row of datagridview!!!
How you changed it? Do you know whta to change?
What is this code:

int rowIndex = dataGridView1.Rows.Add();

This code throws an error!!

Please paste my code into your code, all you have to do to use your own variable names.

Yes. But If I need add more that one row from datatables so I must made what I wrote:

for (int numberofrows = 0; numerofrows <= dTable2.Rows.Count - 1; nymberofrows++)
                        {
                            dataGridView1.Rows.Add();
                            int rowIndex = dataGridView1.Rows.Count - 1;
                            for (int i = 0; i < dTable2.Rows.Count; i++)
                            {
                                for (int j = 0; j < dTable2.Columns.Count; j++) 
                                {
                                    dataGridView1[j, rowIndex].Value = dTable2.Rows[i][j];
                                }
                            }
                        }

And its a problem. I dont know why but in datagrid I have only "0" in first column (must be 08:00 , 08:30 etc) but last row is good. Thats its a problem. If I want only one row its will be workin. If I want to more than one... Chech it please.

Edited 5 Years Ago by deefpi: n/a

Your topic says about one row.
So i have answered on it.

If you want to copy more rows, you 1st have to know which rows exactly?!
Can you answer me on this question

From this code of yours it seems like you want to copy all the rows from DT to DGV.
Is this really what you want?

Yes. you're right. But this is complicated. Sometimes I need add to datagrid more that one rows. And its not a problem. In datatable I found a row and add to datagrid. I know how its working. But I have a problem with adding to datagrid. The best way to add something from database to datagrid is connect to databse and connect it to datagrid.datasource. But when I do this I have problem with datagrid. I cant change any color of cells there. Its funny but its true. I have always a problem:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Maybe its my fault. I working on currentcell in datagrid but its doesnt matter.

Sometimes I want to add to datagrid more than one row. In Your code its a simple:

for (int i = 0; i < dTable2.Rows.Count; i++)
{
dataGridView1.Rows.Add();
int rowIndex = dataGridView1.Rows.Count - 1;
for (int j = 1; j < dTable2.Columns.Count; j++)
{
dataGridView1[j, rowindex].Value = dTable2.Rows[i][j];
}
}

And its working. But I have "0" in my first column (its like 0,0,0,0,0,null and that what they must be - 19:00). In table in database its like that:

Hour || Worker1 || Worker2 || ...
08:00
08:30
09:00
...
19:00

When I want to add this to database I have:

0
0
0
...

19:00

I dont know why but every time I have that error:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Thats a problem. If I make datagrid.datasource = datatable. Its working great. But I cant do some aplication tasks on that datagrid. Sorry If I make a trouble and mistakes. But its first time when something like that is happens to me...

Edited 5 Years Ago by deefpi: n/a

To use the DataSource property of the data grid you must also bind any DataGridColumns you create to the columns of the data table. To do this you set the DataPropertyName property to the name of the bound column in the data table.
If you do not do this then the data grid creates new columns for each column in the data table. (Perhaps this is why you cannot change the colours of the cells when using DataSource.)

Setting cell colours when using a DataSource is best done in the DataGrid.CellFormatting event as the data grid uses this to determine how to display the cell.
The snippet below changes the background for modified cells.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // get bound row for cell being formatted.
            var drv = dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;

            // if row not bound then ignore it
            if (drv == null || drv.Row == null)
                return;

            // get local reference to row
            var row = drv.Row;

            // if row has original and current (new) value then test further
            if (row.HasVersion(DataRowVersion.Current) && row.HasVersion(DataRowVersion.Original))
            {
                // get original and current values
                object original = row[e.ColumnIndex, DataRowVersion.Original];
                object current = row[e.ColumnIndex, DataRowVersion.Current];
                // if original not same as current then change background colour of cell
                if (!current.Equals(original))
                    e.CellStyle.BackColor = Color.PowderBlue;
            }
        }

Thx a lot for help You two! My problem is resolved :)
I have cellsvaluechanged event. And I didnt saw, that he made a errors. If I added something to datagrid cells (update grid etc) he automatically run event valuechanged and theres made a error. So I changed them. And Its working.

Thx a lot for help!

This question has already been answered. Start a new discussion instead.