This is a migrated thread and some comments may be shown as answers.

GridTemplateColumn Syntax Questions

3 Answers 137 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 16 Mar 2016, 07:55 PM
 
<telerik:GridTemplateColumn HeaderText="Quantity" UniqueName="Quantity">
    <ItemTemplate>
        <asp:Label ID="lblQuantity" runat="server"
            Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>'>
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadNumericTextBox ID="txtQuantity" runat="server" DataType="System.Decimal"
            Type="Number" NumberFormat-DecimalDigits="4" />
    </EditItemTemplate>
</telerik:GridTemplateColumn>

 

 

I've made a RadGrid with EditMode="Batch" that is bound to a dataset. I need to create some GridTemplateColumns, such as the above. 3 questions...

 

1) In checking out examples online, at times I've seen Eval above as it is...

Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>'>

...but other times like this...

Text='<%# Eval("Quantity") %>'>

The latter is cleaner, but wondering if the first is recommended for some reason. Or are they identical?

 

2) Also, at times I've seen in the GridTemplateColumn header the attribute DataField and DataType, such as...

DataField="Quantity"

DataType="System.Double"

... but again, not always. Are they necessary? Or does it depend on the situation?

 

3) Do I need to somehow Bind the field to the EditItemTemplate portion?

 

Thanks for the help!

 

3 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 17 Mar 2016, 01:13 PM
Hello Jeremy,

DataBinder.Eval and Eval are essentially the same in your example.
The difference will come when you have many nested controls once inside another. And from the inner most you want to Eval field from the outermost.
Then you can use DataBinder.Eval(Container.Parent.Parent.Parent.... Parent.DataItem , "some field");

DataType will affect the way values are passed in the where clause of the filter expressions

Select Something from.. ..where 'SomeField' < "123,56"
Select Something from.. ..where 'SomeField' < 123.56

Depending on the datasource it will have different effects as:

*Sorting columns as string, instead of as numbers:
"9.5" > "11" as string
9.5 < 11 as decimal
*Passing wrong decimal separator.
*Some datasource will  throw exception that the data is not in correct format.
*aggregates will not work.
*filter item of the grid will have wrong values: "Contains" , "StartsWith", these are practically not suitable for numbers.

It is recommended to always set the correct DataType to avoid all issues described above.

In the EditItemTemplate you need to use the Bind expression
<telerik:RadNumericTextBox .... DbValue="<%# Bind("Quantity") %>">
Also note that it is recommended to use the DbValue instead of just Value property. Because the DbValue of RadNumericTextBox cast the value to the correct type before inserting.

Regards,
Vasil
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 17 Mar 2016, 01:35 PM

Thank you! Makes sense!

From what you said, it appears the Bind part in EditItemTemplate is all I need to tie the column to the field, and there's no need to use DataField in the header. (In the past, when I've used specific column types, like in the example below, I think I needed it. But it's not needed with GridTemplateColumn, correct?)

 

<telerik:GridNumericColumn DataField="Quantity" HeaderText="Quantity"
    UniqueName="Quantity" DataFormatString="{0:N4}" DataType="System.Double">
</telerik:GridNumericColumn>

0
Accepted
Vasil
Telerik team
answered on 17 Mar 2016, 02:42 PM
Hi Jeremy,

When you set-up NumericColumn, the DataField is used to be set to the NumericTextBox that the column adds automatically.

TemplateColumn could be used without DataField. For example your template could have multiple inputs inside for different columns. Or could do something entirely not related to the data.

Sometimes you may still want to set DataField for your template column. For example if you do filtering by this column: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/filtering/filter-template
Or you need to calculate Aggregates:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/aggregates

If you do sorting by this column, you need to set SortExpression:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/sorting/gridhyperlinkcolumn-and-gridtemplatecolumn

Regards,
Vasil
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Jeremy Yoder
Top achievements
Rank 1
Share this question
or