Hello Lee,
The Grid needs the Columns declared on the server-side. This is where the main difference between the SqlDataSource and ClientDataSource comes into play.
The SqlDataSource is a server-side control that allows the Grid to get the data and based on it to generate its columns.
The ClientDataSource is client-side focused control and more often it is configured with client-side data, which is not convenient for the Grid.
Also, the ClientDataSource allows manipulation of returned data and schema on the client-side, so the Schema is not always a reliable source for autogenerating the columns.
Let me know if there is anything still left unclear.
Regards,
Peter Milchev
Progress Telerik
Hi Peter
Yes that works, but I'm wondering if we can have it out of the box to speed up development. Currently the ClientDataSource integration isn't as slick as the regular SqlDataSource integration (which is spot on by the way, it automatically generates the declaritive column definitions from the datasource control)
It would be great if the ClientDataSource could be as easy to integrate.
Cheers
Lee
Hello Lee,
Are you after the opposite generation, from the GridColumns to create the Schema Model?
<Schema>
<Model ID="CustomerID">
<%--<telerik:ClientDataSourceModelField FieldName="CustomerID" DataType="Number" />
<telerik:ClientDataSourceModelField FieldName="CompanyName" DataType="String" />
<telerik:ClientDataSourceModelField FieldName="ContactName" DataType="String" />
<telerik:ClientDataSourceModelField FieldName="ContactTitle" DataType="String" />--%>
</Model>
</Schema>
protected void RadClientDataSource1_Init(object sender, EventArgs e)
{
var clientDataSource = sender as RadClientDataSource;
foreach (GridColumn col in RadGrid1.Columns)
{
if (col is GridBoundColumn)
{
clientDataSource.Schema.Model.Fields.Add(new ClientDataSourceModelField() { FieldName = (col as GridBoundColumn).DataField });
}
}
}
Regards,
Peter Milchev
Progress Telerik
Hi Peter
This isn't quite what i'm after.
Currently, if you attach a RadGrid to a regular DataSource Control you can generate the columns markup in ASPX.
This is useful as it means I can then quickly adjust the DataFormatString, column type etc in the ASPX, not the codebehind.
It would be great if this could be done with the clientdatasource as well
Cheers
Lee
Hello Lee,
The programmatic generation of the columns can be easily achieved in the Init event of the RadGrid, based on the model fields of the ClientDataSource:
protected void RadGrid1_Init(object sender, EventArgs e)
{
var grid = sender as RadGrid;
foreach (ClientDataSourceModelField item in RadClientDataSource1.Schema.Model.Fields)
{
grid.Columns.Add(new GridBoundColumn()
{
HeaderText = item.FieldName,
DataField = item.FieldName
});
}
}
Regards,
Peter Milchev
Progress Telerik