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

Problem referencing dataItem from c#

3 Answers 222 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Andy Green
Top achievements
Rank 2
Andy Green asked on 08 Apr 2016, 01:48 PM

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;

 

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 13 Apr 2016, 07:03 AM
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.
0
Andy Green
Top achievements
Rank 2
answered on 13 Apr 2016, 08:24 AM

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

0
Konstantin Dikov
Telerik team
answered on 14 Apr 2016, 01:37 PM
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.
Tags
Scheduler
Asked by
Andy Green
Top achievements
Rank 2
Answers by
Konstantin Dikov
Telerik team
Andy Green
Top achievements
Rank 2
Share this question
or