views:

73

answers:

2

I am populating a datagridview with objects like this:

        foreach (NavField field in this.Fields)
        {
            DataGridViewColumn column = new DataGridViewColumn();
            column.HeaderText = field.Caption;
            column.Name = field.FieldNo.ToString();
            column.ValueType = field.GetFieldType();
            column.CellTemplate = new DataGridViewTextBoxCell();

            grid.Columns.Add(column);
        }

        int count = 0;
        foreach (NavRecord record in this.Records)
        {
            grid.Rows.Add();
            foreach (NavItem item in record.items)
            {
                //Adding the rows
                grid.Rows[count].Cells[item.FieldNo.ToString()].Value = item.RecordValue;

            }
            count++;
        }

But the datasource of the grid stays null. Is there any other solution to populate the grid, or update the datasource?

A: 

Create DataTable and bind it to DataGrid .

dataGrid.DataSource = dataTable
Orsol
In the second loop I have some other conditions to be meet in order to add the row (which I have not included in the question). Moreover, the rows are mapped on the fields according to some condition, not only index of the column. For the datasource, I thought that we could push the datagridview to the datasource. Is that possible? I did not use datatable as datatable does not map correctly on the datagridview there are things like headertext which is caption in datatable or hide columns which is done differently.
Kervin
+1  A: 

The DataSource is null because you never set it.

You can simply set grid.DataSource = this.Records and skip the second foreach loop completely. This would require that you also set the DataPropertyName of each column you add to the grid.

This might not work with your custom NavRecord class without some modifications, though (can't say for certain unless you post the code for this as well).

foreach (NavField field in this.Fields)
{
    DataGridViewColumn column = new DataGridViewColumn();
    column.HeaderText = field.Caption;
    column.Name = field.FieldNo.ToString();
    column.ValueType = field.GetFieldType();
    column.CellTemplate = new DataGridViewTextBoxCell();
    // added:
    column.DataPropertyName = field.FieldNo.ToString();

    grid.Columns.Add(column);
}

grid.DataSource = this.Records;

Oh, and you might want to set grid.AutoGenerateColumns = false; otherwise each column might appear twice when using this approach.

Bernhof
Grid.Rows and Grid.Columns are different from DataTable rows and columns.When you are manipulation Grid.Rows and Grid.Columns you only manipulate layout.If you bind data to DataGrid, you had to set data as Bernhof said
bahadir arslan
In the second loop I have some other conditions to be meet in order to add the row (which I have not included in the question). Moreover, the rows are mapped on the fields according to some condition, not only index of the column. For the datasource, I thought that we could push the datagridview to the datasource. Is that possible?
Kervin
I'm not sure what you mean by pushing the datagridview to the datasource - could you elaborate?
Bernhof
If you're talking about reading the data you are inserting in grid.Rows and grid.Columns through the DataSource property, then no - that's not possible.
Bernhof
@Bernhof, that is what I wanted to do. But what other datasources alternatives are there to populate a datagridview, considering I have my data in objects. I tried to datatable, it works but the columns are not mapped correctly. Datatable has caption and datagridview has headertext.
Kervin
The solution I am using currently is that I am modifying the displayed datagridview directly and the ui is slow. That is why I wanted to instanciate a datagridview, populate it and then assign its datasource to the ui datagridview.
Kervin