IT Forward
IT Forward

Reputation: 395

How do I solve the Exception with message: "A column named 'Name' already belongs to this DataTable"?

I am getting the following error which says; A column named 'Name' already belongs to this DataTable. how can i sove this error, i tried to look for some solutions but i cant get it working

private DataTable GetDataTableFromDataGridview(DataGridView _grid)
    {
      {
            var _oDataTable = new DataTable();
            object[] cellValues = new object[_grid.Columns.Count];
            foreach (DataGridViewRow row in _grid.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    clearTable();
                    _oDataTable.Columns.Add("Name", typeof(string)); //error here
                    _oDataTable.Columns.Add("Value", typeof(string));
                    _oDataTable.Columns.Add("Font", typeof(string));
                    _oDataTable.Columns.Add("DateStamp", typeof(string));
                    _oDataTable.Columns.Add("Comment", typeof(string));
                    cellValues[i] = row.Cells[i].Value;
                }
                _oDataTable.Rows.Add(cellValues.ToArray());
            }
            return _oDataTable;

        } 
     public void clearTable()
    {
        DataRow _datarow;
        oDataTable.Clear();
        _datarow = oDataTable.NewRow();
        Gridview_Output.DataSource = oDataTable;
    }

    }

Upvotes: 0

Views: 25310

Answers (2)

keitn
keitn

Reputation: 1298

You should only add columns for the first row or move the column addition to outside the loop.

private DataTable GetDataTableFromDataGridview(DataGridView _grid)
    {
      {
            var _oDataTable = new DataTable();
            object[] cellValues = new object[_grid.Columns.Count];
            foreach (DataGridViewRow row in _grid.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                   if (i ==0)
                   {
                    clearTable();
                    _oDataTable.Columns.Add("Name", typeof(string)); //error here
                    _oDataTable.Columns.Add("Value", typeof(string));
                    _oDataTable.Columns.Add("Font", typeof(string));
                    _oDataTable.Columns.Add("DateStamp", typeof(string));
                    _oDataTable.Columns.Add("Comment", typeof(string));
                   }
                    cellValues[i] = row.Cells[i].Value;
                }
                _oDataTable.Rows.Add(cellValues.ToArray());
            }
            return _oDataTable;

        }

}

Upvotes: -1

Justin Harvey
Justin Harvey

Reputation: 14672

Add the columns just once before the loop.

        clearTable();
        _oDataTable.Columns.Add("Name", typeof(string)); //no error here
        _oDataTable.Columns.Add("Value", typeof(string));
        _oDataTable.Columns.Add("Font", typeof(string));
        _oDataTable.Columns.Add("DateStamp", typeof(string));
        _oDataTable.Columns.Add("Comment", typeof(string));
        foreach (DataGridViewRow row in _grid.Rows)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                cellValues[i] = row.Cells[i].Value;
            }
            _oDataTable.Rows.Add(cellValues.ToArray());
        }

Upvotes: 3

Related Questions