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

Scheduler Data Binding To DataGrid

1 Answer 52 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Geoffrey
Top achievements
Rank 1
Geoffrey asked on 22 Dec 2008, 05:17 PM
Hello All,
   I'm new to C# and to Telerik.  My problem is that I'm modifying the supplied code to bind a datagrid to the scheduler's data.  Heres my code:

//Create a dynamic table to store the appointments' information:  
        DataTable table1 = new DataTable();
        table1.Columns.Add("ID");
        table1.Columns.Add("Instructor");

        foreach (Appointment a in RadScheduler1.Appointments)
        {
            if (a.RecurrenceState == RecurrenceState.Master)
            {
                RecurrenceRule parsedRule;
                RecurrenceRule.TryParse(a.RecurrenceRule.ToString(), out parsedRule);

                //If a recurring appointment does not have specified end time it will have  
                //endless occurrences. In this case you can explicitly put a restriction:  
                //parsedRule.SetEffectiveRange(DateTime.Now.AddYears(-1), DateTime.Now.AddYears(1));  
                foreach (DateTime occurrence in parsedRule.Occurrences)
                {
                    table1.Rows.Add(new string[] { a.Subject.ToString(), occurrence.ToString(), occurrence.Add(a.Duration).ToString() });
                }
            }
            else
                if (a.RecurrenceState != RecurrenceState.Occurence)
                {
                    //table1.Rows.Add(new string[] { a.Attributes["ID"].ToString(), a.Attributes["Instructor"].ToString()  });

                    string x = a.Attributes["Instructor"].ToString();

                    table1.Rows.Add(new string[] { a.Subject.ToString() });
                }
        }
        GridView1.DataSource = table1;
        GridView1.DataBind();
    }


The problem is that I get a NULL reference exception when trying to include the "Attributes" to the datagrid.  For instance:
   
table1.Rows.Add(new string[] { a.Subject.ToString() });  works, but
table1.Rows.Add(new string[] { a.Attributes["ID"].ToString() }); doesn't work.  Why?? And how do I get around it??





1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 23 Dec 2008, 10:29 AM
Hello Geoffrey,

Have you set CustomAttributeNames="ID" for RadScheduler? If yes, then you should be able to get the ID like this: a.Attributes["ID"]. If not, then you can directly access the ID like this: a.ID.ToString(), provided that you have set DataKeyField="ID".


Greetings,
Peter
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Scheduler
Asked by
Geoffrey
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or