0

Here is method which copy DataTable into DataGridView which is not not working, this method only able to add columns and empty rows in DataGridView. Can any one suggest me solution for this without using DataSource property of DataGridView?

    public void CopyDataTableInDataGridView(DataTable table, DataGridView gdv)
    {
        if (gdv.Rows.Count > 0)
            gdv.Rows.Clear();

        if (table != null && table.Rows.Count > 0)
        {
            foreach (DataColumn _colm in table.Columns)
            {
                DataGridViewColumn _col;

                if (_colm.GetType() == typeof(bool))
                    _col = new DataGridViewCheckBoxColumn();
                else
                    _col = new DataGridViewTextBoxColumn();

                _col.Name = _colm.ColumnName;
                _col.HeaderText = string.Concat(_colm.ColumnName.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
                gdv.Columns.Add(_col);
            }

            foreach (DataRow _row in table.Select())
            {
                //Rows getting added in dgv but not data
                // By adding following line in Code my problem get solved
                //object[] _items = _row.ItemArray;
                gdv.Rows.Add(_row);
            }
        }
    }
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • Why do you *not* want to use the `DataSource` property? – gunr2171 Aug 21 '13 at 14:54
  • @gunr2171 Because I want to make number of operations on DataGridView but DataSource restrict me to do so.. – Ankush Madankar Aug 21 '13 at 14:58
  • Not really sure why you don't want to use `DataSource` property. Currently you are adding each row's `ToString` implementation, instead you should add an array of data coming out of each column for the row. You can see at [this question](http://stackoverflow.com/questions/10063770/c-sharp-how-to-add-a-new-row-to-datagridview-programmatically/10063825#10063825) for adding rows in the grid view through code – Habib Aug 21 '13 at 14:58
  • @AnkushMadankar, I don't understand how `DataSource` would restrict you. Remember that you can pass in a custom `class`, not just a `DataTable`, as the data. You can format the data in the class first, then insert the class. – gunr2171 Aug 21 '13 at 15:02

2 Answers2

0

You are trying to add a DataRow to DataGridView instead of adding DataGridViewRow

Look at what visualstudio's intelisence is telling you about DataGridView.Rows.Add() methods. There are 4 of them:

  • Add() - adds an empty row to DataGridView

  • Add(DataGridViewRow) - adds new row (this is what you need)

  • Add(count) - adds [count] of empty rows to DataGridView

  • Add(object[]) adds new row and populates it with values (you can also use this)

you are currently using the last one: Add(object[]). compiler doesn't complain because it's treating DataGridViewRow you passed to it as an array of objects with only one object in it. Obviously not what you want.

here is related question: https://stackoverflow.com/a/9094325/891715

Community
  • 1
  • 1
Arie
  • 5,251
  • 2
  • 33
  • 54
0

By adding following line before adding row in DataGridView my problem get solve.

   object[] _items = _row.ItemArray;
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74