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

How to get range of radgrid field

3 Answers 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
york
Top achievements
Rank 1
york asked on 21 Jun 2011, 07:34 AM

Hi,

I want to have range on data field in DataTable in RadGrid, such DateTime, int, etc. How to get the range of them, such as min and max datetime and int?

Thanks.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Jun 2011, 11:23 AM
Hello York,

I suppose you have two date fields in your RadGrid and you want to get the difference between these fields. If that is the requirement, try the following code snippet in ItemDataBound event.

aspx:
<telerik:GridTemplateColumn UniqueName="temp">
    <ItemTemplate>
      <asp:Label ID="lbl" runat="server"></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            Label lbl = (Label)item.FindControl("lbl");              
            string Birthdate= (item["ColumnUniqueName1"].Text);
            string HireDate = (item["ColumnUniqueName2"].Text);
            int date = Convert.ToDateTime(BirthDate).Subtract(Convert.ToDateTime(Hiredate)).Days;      
            lbl.Text = date.ToString();                              
        }
}


Thanks,
Princy.
0
york
Top achievements
Rank 1
answered on 21 Jun 2011, 09:54 PM
No, I just want to have max and min value of radgrid column. For example, if column is datetime, I want the latest and earliest date and time for the column. I think that they may be stored in mastertableview already because it can be sorted. I know how get max and min value through iteration of all rows but it is slow.
0
Accepted
Veli
Telerik team
answered on 24 Jun 2011, 07:48 AM
Hi york,

If you do not use a separate DB query to retrieve the max and min values for a particular field, you do not have an option but to loop through your data items in RadGrid. You can use a handy event in this case - ItemDataBound. This event is fired for every data item in RadGrid when the respective item is databound. You can use the event handler to sequantially store the max and min values encountered so far. By the time all items are databound, your min and max values will contain their final values. Note that this scenario will work only if you do not have paging, or you want to retrieve the min/max values for the current page of items only. If you have paging and you want to retrieve the min/max among all grid items (not only in the current page), this approach will not work, as RadGrid does not create items for data that will not appear in the current page. If you want to try this option, here is a sample ItemDataBound event handler:

DateTime minValue;
DateTime maxValue;
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if(e.Item is GridDataItem)
    {
        DateTime dateValue = (DateTime)DataBinder.Eval(e.Item.DataItem, "MyDateField");
        minValue = Math.Min(minValue, dateValue);
        maxValue = Math.Max(maxValue, dateValue);
    }
}


Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
york
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
york
Top achievements
Rank 1
Veli
Telerik team
Share this question
or