循环 dataRow 的每个项目并按主键列对它们进行分组

Loop on each item of a dataRow and group them by a primary key column

我正在从 SQL 服务器返回一个包含约会的数据行:主题、开始日期、结束日期和频道 ID。

我需要循环 dataRow 的每个项目,以便能够将特定频道 ID 的项目单独分组,以便能够将它们添加到 ultraGanttView。

以下代码可以正确地将数据绑定到 UltreMonthViewSingle。

所以我需要能够将数据绑定到 ganttView 中的是按项目对它们进行分组,在我的例子中是频道 ID。

 private void FillCalendar()
    {
        string query = @"select rs.[Planned Date in] as pdin,rs.[Planned Date out] as pdout, CONCAT(cn.Name,' ',ps.[First Name],' ',ps.[Last Name]) as subj, cn.[ID]
                        from [dbo].[Reservations] rs
                        inner join [dbo].[Person] ps on rs.[Person ID] = ps.ID
                        inner join [dbo].[Channel] cn on rs.[Channel ID] = cn.ID";
        SqlConnection conn = new SqlConnection(Utilities.ConnectionString);
        conn.Open();
        //return reservations datatable
        DataTable table = new DataTable();
        SqlDataAdapter adp2 = new SqlDataAdapter(query, conn);
        adp2.Fill(table);

        //bind appointments
        Appointment appointment;
        DateTime dateIn, dateOut;
        String subj;

        foreach (DataRow dataRow in table.Rows)
        {
            dateIn = DateTime.Parse(dataRow["pdin"].ToString());
            dateOut = DateTime.Parse(dataRow["pdout"].ToString());
            dateIn.ToString("dd-MMMM-yyyy");
            subj = dataRow["subj"].ToString();
            appointment = this.ultraCalendarInfo1.Appointments.Add(dateIn, dateOut, subj);
        }

    }

我应该在循环中添加什么才能使其正常工作?

这是一份如何在甘特视图中绑定数据的文档GanttView Binding

谢谢

这就是我所做的并且正在运行。

 private void BindGantt()
    {
        this.ultraGanttView1.CalendarInfo = this.ultraCalendarInfo1;
        string query = "select rs.[Channel ID],c.Name from[dbo].[Reservations] rs inner join[dbo].[Channel] c on rs.[Channel ID] = c.ID group by rs.[Channel ID],c.Name";
        DataTable dt = new DataTable();
        DataTable dc = new DataTable();
        SqlConnection conn = new SqlConnection(Utilities.ConnectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = query;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        DateTime dateFrom;
        TimeSpan duration;
        string subject;

        int id;
        //get the channels' id that have reservations 
        for (int j = 0; j < dt.Rows.Count; j++)
        {
            id = Convert.ToInt32(dt.Rows[j].ItemArray[0].ToString());  
            dc = GetChannelsReservations(id);
            //get the info of all the reservations for a channel
            ultraCalendarInfo1.Tasks.Add(DateTime.Now, TimeSpan.FromDays(0), dt.Rows[j].ItemArray[1].ToString());
            for (int i = 0; i < dc.Rows.Count; i++)
            {
                dateFrom = Convert.ToDateTime(dc.Rows[i].ItemArray[0]);
                TimeSpan.TryParse(dc.Rows[i].ItemArray[1].ToString(),out duration);
                subject = dc.Rows[i].ItemArray[2].ToString();
                ultraCalendarInfo1.Tasks[j].Tasks.Add(dateFrom, duration, subject);     
            }
        }
    }
    private DataTable GetChannelsReservations(int id) {
        string query = @"select rs.[Planned Date in],DATEDIFF(DAY, rs.[Planned Date in],rs.[Planned Date out]) as TimeSpan,c.Name+' '+p.[First Name]+' '+p.[Last Name],c.Name
                        from[dbo].[Reservations] rs
                        inner join[dbo].[Channel] c on rs.[Channel ID] = c.ID
                        inner join[dbo].[Person] p on rs.[Person ID] = p.ID
                        where rs.[Channel ID] ="+id;
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(Utilities.ConnectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = query;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        return dt;
    }