DataGridViewColumn.DataPropertyName Property

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?

-------------Problems Reply------------

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

I like the solution that uses the CellFormatting event.

Problem nr.1:

Try to do the following:

  1. extend MyOtherClass from Object (this step might not be needed)
  2. and override, or create, method ToString().

That should do it.

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);

Category:winforms Views:4 Time:2010-02-08

Related post

  • C# Windows Form -DataGrid -DataPropertyName 2009-12-15

    What is the use of DataPropertyName in Win Form of VS? --------------Solutions------------- DataGridViewColumn.DataPropertyName Property Gets or sets the name of the data source property or database column to which the DataGridViewColumn is bound. Wh

  • Bind named datagridview column with specific column in datatable 2012-04-28

    I added some columns into datagridview by using the following code: dataGridView1.Columns.Add("name"); dataGridView1.Columns.Add("age"); dataGridView1.Columns.Add("salary"); I have datatable that contains data from stored procedure select col1,col2,c

  • Binding DataGridView to a List, some properties Should not be shown 2009-05-22

    I am trying to bind a DataGridView to a List, where MyObject looks like class MyObject { public string Property1 { get; set; } public string Property2 { get; set; } } //List<MyObject> objects; grid.Columns[0].DataPropertyName = "Property1"; gri

  • C# WinForms DataGridView - Constant Row Selected! 2009-07-06

    I have a winforms datagridview that seems to always have at least one row selected all the time. I'm not interested in being able to select the rows at all really, I just need the user to be able to select the checkbox in column 1. Any ideas why ther

  • Add own columns in a binded DataGridview 2010-01-18

    I have a DataGridView that gets a datasource assigned. I would like to create my own columns if it's (for example) DateTime. I found an example of how you can create a DateTimePicker (here) (and hopefully also a NumericUpDown) to add to the datagrid,

  • DataGridView - how to set column width? 2010-01-28

    I have a WinForms application with DataGridView control. My control has five columns (say "Name", "Address", "Phone" etc) I am not happy with default column width. I want to have more control over column appearance. What I want is to be able to do on

  • C# Bind DataTable to Existing DataGridView Column Definitions 2010-04-28

    I've been struggling with a NullReferenceException and hope someone here will be able to point me in the right direction. I'm trying to create and populate a DataTable and then show the results in a DataGridView control. The basic code follows, and E

  • How to create a list of data from a class 2011-04-10

    I have a class 'Relation' with property as Child column and parent column.The property child column has details in another class 'Columns' with property such as table name and column name.I need to display the child column_table name and column name

  • Does DataGridView always depend on public properties for column values? 2011-05-26

    At runtime, I have a collection of rows (Row class). Each of them consist of column values, represented by instances of a ColumnValue class. The name of the columns are determined at runtime, and are in a separate columns descriptor collection (Colum

  • How to set the width of a column in a datagrid to * 2011-08-22

    I have got datagrid with few columns, and I want to have one of them always fill empty space of this datagrid. Hwo can I change this code to have this result? Unfortunately this is not my code <data:DataGrid x:Name="grdArchive" Grid.Row="0" Visibi

  • At least one of the DataGridView control's columns has no cell template 2011-12-10

    I'm getting that exception. System.InvalidOperationException was unhandled Message=At least one of the DataGridView control's columns has no cell template. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.DataGridView.CompleteCellsColl

  • binding datagridview to [Browsable(false)] columns 2012-02-23

    I have a a datagrid that I am binding to class that has some columns marked [Browsable(false)]. These columns are showing up blank. Is there some way to turn this off. I am not using any autobinding but creating the columns myself and setting the Dat

  • Gridview link button click event - Windows application 2008-12-27

    How to handle click event of the linkbutton in gridview in (windows application ) Thanks --------------Solutions------------- You need to consume the DataGridView.CellClick Event and just check that you've got the correct column index. You can

  • Changing column order in DataTable bound to DataGridView does not reflect in the view 2009-01-31

    When the application is run, the DataGridView is bound to a DataTable. Later I add more columns to the DataTable programmatically and it is reflected in the underlying data - i.e. the column Ordinals are as they should be. However this is not reflect

  • Databinding with a custom object to datagridview - Column Header Text 2009-06-11

    I have an list of objects (PrintJob) that I bind to a DataGridView. Here is a cut down version of the PrintJob object (Don't want to bore you completely!!): public class PrintJob { private long pagesToPrint; public long PagesToPrint { get { return pa

  • How to bind list to dataGridView? 2009-08-04

    I seem to be running around in circles and have been doing so in the last hours. I want to populate a datagridview from an array of strings. I've read its not possible directly, and that I need to create a custom type that holds the string as a publi

  • Show Attribute of Relation in DataGridView 2009-08-27

    I want to show the attribute of a relation in a DataGridView. I use LINQ to SQL to do the mapping and have the following classes: class Computer { public string Name; public User User; } class User { public string Name; } I use a DataGridView to show

  • Get DataGridView columns in order of user's ordering 2009-10-22

    I have an app that lets the user reorder columns in a DataGridView, and I want to save the "layout" upon closing the app...but it seems that when I iterate through the column collection, I get them in the order I added them, not the order they appear

  • DataGridView - how freeze a column? 2009-12-14

    Using DataGridView with VB.Net 2008. I would like to freeze the left column so that when/if scrolled this column still remains in place. can someone tell me how to do this? --------------Solutions------------- How to: Freeze Columns in the Windows Fo

Copyright (C), All Rights Reserved.

processed in 0.162 (s). 11 q(s)