user559142
user559142

Reputation: 12517

Binding Object Properties to Specific Telerik RadGridView Columns

I am defining the data source of a Telerik RadGridView control in its NeedDataSource event. The data source in question is an IEnumerable collection of objects.

Say, for example, the column order in the RadGridView is Title | Description | Date. If I have the following objects in my collection...

class Obj{

  public DateTime Date {get;set;}
  public string Title {get;set;}
  public string Description {get;set;}

}

How do I ensure when using RadGridView1.DataSource();that the object property values are assigned to the RadGridView columns in the correct order?

Upvotes: 0

Views: 1321

Answers (2)

TKTS
TKTS

Reputation: 1261

If you are auto generating the columns and would like to reorder them programatically, you can use the SwapColumns method discussed here. The columns should be added automatically in alphabetical order, so just calling the below should work.

grid.MasterTableView.SwapColumns("Date","Title");

It worked for me to put this in the DataBound event handler.

Upvotes: 0

msigman
msigman

Reputation: 4524

I'm not sure I fully understand your question. Are you using Auto generate columns at runtime? If not: the column order is defined by you declaratively, like this:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView DataKeyNames="ObjId">
                 <Columns>
                        <telerik:GridBoundColumn UniqueName="Date" HeaderText="Date" DataField="Date" DataFormatString="{0:M/dd/yyyy}">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Title" HeaderText="Title" DataField="Title">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" DataField="Description">
                        </telerik:GridBoundColumn>
                    </Columns>
    </MasterTableView>
    </telerik:RadGrid1>

Notice that the DataField is telling the RadGrid which object property to use. If I've completely missed your question, let me know.

Upvotes: 1

Related Questions