Problem referencing dataItem from c#

4 posts, 0 answers
  1. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 08 Apr Link to this post

    Hi

    Just need some help converting a VB grid to C#.

    Binding is to a datatable deserialised from JSON using NewtonSoft.Json library.

    I have the following line in VB that works OK:

    If e.Appointment.DataItem("UsageType") = 1 Then...

    However the same line in C# 

    if (e.Appointment.DataItem("UsageType") == 1) {

    Errors with a red squiggle under DataItem with the following text:-

    SchedulerEventArgs does not contain a definition for Dataitem and no extension method DataItem accepting a first argument of type SchedulerEventArgs could be found.

    I have the following using statements:

    using Telerik.Web.UI;
    using Telerik.Web;
    using System.Data;

     

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 13 Apr Link to this post

    Hi Andy,

    The error that you are receiving indicates that you are trying to access (somewhere in your code) the DataItem through the event arguments directly. As for retrieving the value in question, since you are binding the control to DataTable you need to cast the DataItem to the corresponding type first (DataRow or DataRowView):
    (e.Appointment.DataItem as DataRowView).Row["UsageType"]

    If the above fails, you can debug your code and see the type of your DataItem.

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 13 Apr in reply to Konstantin Dikov Link to this post

    Hi

    you are trying to access (somewhere in your code) the DataItem through the event arguments directly  YES in the AppointmentDataBound event, this  e.Appointment.DataItem("UsageType") works OK in VB, but not C#.

    This is the way I'm trying to use it:

    e.Appointment.BackColor = System.Drawing.ColorTranslator.FromHtml((e.Appointment.DataItem as DataRowView).Row["Appointmentcolour"]);

    The whole line is underlined, and the dataitem on hover says - Object Appointment.Dataitem{get; Set;} gets or sets the dataitem represented by tge appointment object in the radscheduler control. Canot convert from object to string.

    Andy

  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 14 Apr Link to this post

    Hello Andy,

    As the error states, you need to pass string value to the FromHtml method, so you can try the following:
    e.Appointment.BackColor = System.Drawing.ColorTranslator.FromHtml((e.Appointment.DataItem as DataRowView).Row["Appointmentcolour"].ToString());

    Please let me know if that resolves the issue.


    Regards,
    Konstantin Dikov
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top