Binding Telerik RadGRID to a list<string> object

alchemical picture alchemical · Feb 13, 2009 · Viewed 21.3k times · Source

I have a simple use for RadGrid that involves binding it to a list of strings

  i.e. using:  list<string>

The bind works OK and the data displays in the grid. However, the header says "Item", and there are other aspects of the column I'd like to be able to customize. I've tried to set the "DataField" property of the column on the ascx page:

    <telerik:GridTemplateColumn UniqueName="column" 
DataField="" HeaderText="Omniture Codes">

however, it seems to want the name of a data field, as in what you would get with a datatable object, but not with a list.

Does anyone know a way to bind the column to the list, or have another idea for a work-around?

Answer

M4N picture M4N · Feb 13, 2009

I think you should use a GridBoundColumn instead of the GridTemplateColumn and disable AutoGenerateColumns.

E.g. the following works for me:

ASPX:

<telerik:RadGrid ID="grid" runat="server" AutoGenerateColumns="false">
  <MasterTableView>
    <Columns>
      <telerik:GridBoundColumn DataField="" HeaderText="MyHeaderText">
      </telerik:GridBoundColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    List<string> data = new List<string> {"a", "b", "c"};
    grid.DataSource = data;

}