views:

403

answers:

3

Hi

I have a DataGridView control and I want to populate it with data.

I use DataSource property

// dgvDealAsset is DataGridView
        private void DealAssetListControl_Load(object sender, EventArgs e)
        {
            dgvDealAssets.AutoGenerateColumns = false;
            dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();
}

Now problem number one. The class of my collection does not contain only simple types that I can map to columns using DataPropertyName. This is the class that is contained in collection.

class MyClass
{
  public String Name;
  MyOtherClass otherclass;
}

class MyOtherClass
{
 public String Name;
}

Now I am binding properties of MyClass to columns

col1.DataPropertyName = "Name"  // Ok 
col2.DataPropertyName = "otherclass" // Not OK - I will have empty cell

The problem is that I want to display otherclass.Name field. But if I try to write

col2.DataPropertyName = "otherclass.Name" 

I get empty cell.

I tried to manually set the column

private void DealAssetListControl_Load(object sender, EventArgs e)
{
    dgvDealAssets.AutoGenerateColumns = false;
    dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();

// iterate through rows and set the column manually
        foreach (DataGridViewRow row in dgvDealAssets.Rows)
        {
            row.Cells["Column2"].Value = ((DealAsset)row.DataBoundItem).otherclass.Name;
        }

But this foreach cycle takes about minute to complete (2k elements). How to solve this problem?

+1  A: 

Problem nr.1:

Try to 1. extend MyOtherClass from Object (this step might not be needed) 2. and override, or create, method ToString(). That should do it.

Cannot do. This is third-party library object.
Captain Comic
If MyOtherClass is not sealed then try this: class MyOtherClass2 : MyOtherClass { public string ToString() { /*return string here which you want to appear in the grid*/ } }
+1  A: 

DataGridView doesn't support databinding to child properties. For more info, check this post

I like the solution that uses the CellFormatting event.

Jacob Seleznev
+1  A: 

It sounds like the DataGridView's virtual mode would solve your problem. In virtual mode, the DataGridView will fire an event whenever it needs to display a cell. The event lets you populate the cell however you please. The advantage of virtual mode is the system only needs to pull the data that's actually being displayed, so there's no slow start-up time while you load everything.

    private void my_init_function() {
        datagridview.VirtualMode = true;
        datagridview.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(datagridview_CellValueNeeded);
    }

    private void datagridview_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        e.Value = get_my_data(e.RowIndex, e.ColumnIndex);
    }
Daniel Stutzbach