Chibueze Opata
Chibueze Opata

Reputation: 10034

Is there anyway to bind only specific values of a class to a datagrid?

Sometimes, I usually create a class with the type of data I want like this:

public class message{
    private string subject { get; set; }
    private string message { get; set; }
}

This way, I can bind a List<message> to a datagrid and change the DataPropertyName.

However, if I happen to add more auto properties to this class, my datagrid starts to misbehave and sometimes I don't just want some of the properties to the datagrid. Is there anyway I can bind the datagrid to only the values I selected DataPropertyName for?

Thanks.

Upvotes: 0

Views: 1010

Answers (2)

GrayFox374
GrayFox374

Reputation: 1782

If you don't want extra columns in your datagrid, why not start with your query, and don't list them in the SELECT clause. This has the added benefit of reducing network traffic.

Create multiple queries or views off the same table and use the right one at the right timefor your need. This is the "upstream" solution.

Alternatively,

When the AutoGenerateColumns property is set to true, each column automatically sets its DataPropertyName property to the name of a property or database column in the data source specified by the DataSource property. This binding can also be performed manually, which is useful when you want to display only a subset of the properties or database columns available in the data source. In such cases, set the AutoGenerateColumns property to false, and then manually add each DataGridViewColumn, setting the value of each DataPropertyName property to the properties or database columns in the data source that you want to display.

That is straight from MSDN, you should have researched a little more thoroughly before posting this. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx

Upvotes: 2

krillgar
krillgar

Reputation: 12815

You can create a method to provide a DataView.

Say you have something like this:

public class message {
    private string subject { get; set; }
    private string message { get; set; }
    private string otherValue { get; set; }
}

Then you can add a method like this to the message class:

public static DataView GetDataGridList(List<message> lstMessages) {
    DataTable dt = new DataTable();
    // Add the columns here for whatever properties you want
    dt.Columns.Add("subject");
    dt.Columns.Add("message");

    foreach (message msg in lstMessages) {
        DataRow dr = dt.NewRow(); // I think that's the call, I'm doing this off the top of my head, sorry.
        dr["subject"] = msg.subject;
        dr["message"] = msg.message;
    }

    return (dt.DefaultView);
}

Then when you create your DataGrid, just bind it like this:

List<message> lstMessages = new List<message>();
// Populate the list however you want here.
DataGrid dg = new DataGrid();
dg.DataSource = message.GetDataGridList(List<message>);

You should be good to go. Just remember if you change what properties you want, to add the columns to the DataTable, then inside the foreach loop.

Upvotes: 0

Related Questions