2

I have a gridControl who's data source is a List. Each item in List is made out of three fields. And I have 3 columns in the gridControl. when I programatically insert values into the gridview it does not appear. Here is my code.

public Company_Selection()
{
    InitializeComponent();
    companies = new List<DataLibrary.Companies>();
    gridControl1.DataSource = companies;
}

internal void load_all_companies(List<DataLibrary.Companies> other)
{
    for (int i = 0; i < other.Count; i++)
    {
        companies.Add(other[i]);
        gridView1.SetRowCellValue( i, "Id", other[i].id);
        gridView1.SetRowCellValue( i, "Company", other[i].name);
        gridView1.SetRowCellValue(i, "Description", other[i].description);
        gridView1.RefreshData();
        gridControl1.RefreshDataSource();
    }
}

Any ideas about what's wrong ?

Fares
  • 354
  • 3
  • 5
  • 15

4 Answers4

1

I think you for get to write DataBind method

gridControl1.DataBind();

so you code will be

companies = new List<DataLibrary.Companies>();     
gridControl1.DataSource = companies;
 gridControl1.DataBind();

GridView.DataBind Method : Binds the data source to the GridView control.

Use the DataBind method to bind data from a data source to the GridView control. This method resolves all data-binding expressions in the active template of the control.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

You correcly set the datasource property, but you have also to bind the datasource to the control by using the DataBind method like this

gridControl1.DataSource = companies;
gridControl1.DataBind();
Angelo Badellino
  • 2,141
  • 2
  • 24
  • 45
1

The thing is you should not work with cells directly. Your 'companies' list should be enclosed in BindingList. All programmatical changes are done at object, not grid level. So, setting datasource becomes

gridControl1.DataSource = new BindingList<DataLibrary.Companies>(companies);

This will take care of presentation, changes, additions and deletions of objects within list. The columns will be created automatically if your gridView does not contain any and AutoPopulateColumns is true. You might want to setup columns in gridView using Designer. Don't forget so set Field property to Property name of underlying object.

Nikola Markovinović
  • 18,963
  • 5
  • 46
  • 51
0

I believe the cause of the issue is that you are using fieldnames like "Id","Company", "Description" but the grid is mapped on "id","name","description" fieldnames:

gridView1.SetRowCellValue( i, "Id", other[i].id);
gridView1.SetRowCellValue( i, "Company", other[i].name);
gridView1.SetRowCellValue(i, "Description", other[i].description);

Also passing row cell values directly to a view and refreshing the grid after appending every record is very redundant. The single RefreshDataSource method call after data loading is only enough:

internal void load_all_companies(List<DataLibrary.Companies> other) {
    for(int i = 0; i < other.Count; i++) 
        companies.Add(other[i]);
    gridControl1.RefreshDataSource();
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53