试图从一个数据表中检索数据,我试图使用C#基于某些条件填充另一个数据表我无法这样做

时间:2015-06-26 13:35:24

标签: c# datatable

我正在使用以下代码。

我在dta中获得了数据表数据。

现在在GCSdt1中,我想填充数据,以便为每个员工显示用户在该月的列中的班次。我的意思是说该表应该看起来像

Name | 6-1-2015|6-2-2015|.........|6-30-2015下一行应该在名称下面显示名称,在6-1-2015下它应显示他在6-1-2015的班次

以下是我的代码。我可以直到填充第一个数据表并为网格创建列。现在我被卡住了。

    SqlDataReader GCSrdr;
    SqlCommand GCScmd;
    DataRow GCSdrow;
    DataView GTView;
    DataTable GCSdt = new DataTable();
    DataTable GCSdt1 = new DataTable();
    SqlConnection GCScon = new SqlConnection("MYCOnnection");
    DateTime dt = DateTime.Today.Date;
    static DateTime today = DateTime.Today;

    DataColumn[] day1;



    static int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

    DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);
    DateTime endOfMonth = new DateTime(today.Year, today.Month, daysInMonth);

    public DataTable MonthView()
    {

        string gcsCmdSelect = "Select S.Emp_Name,S.User_Shift,S.Shift_Emp_Status,S.Shift_Date from Shift_Details S INNER JOIN Emp_Details E ON S.EMP_Name=E.Employee_Name where (S.Shift_Date >='" + startOfMonth + "') and (S.Shift_Date <='" + endOfMonth + "')  ORDER BY S.Emp_Name";

        if (GCScon.State == ConnectionState.Closed)
        {
            GCScon.Open();
        }
        SqlDataAdapter da = new SqlDataAdapter(gcsCmdSelect, GCScon);
        DataTable dta = new DataTable();
        da.Fill(dta);
       /////// Results fine


        DataView ndtv = new DataView(dta);
        GCSdt = ndtv.ToTable(true,dta.Columns[0].ToString());


        GCSdt1.Columns.Add("Name");
        for (int i = 1; i <=daysInMonth; i++)
        {

            GCSdt1.Columns.Add(startOfMonth.ToShortDateString());
            startOfMonth = startOfMonth.AddDays(1);
        }

          GCSdt1.Rows.Add();
这是////////////////////// p>
        return GCSdt1;


    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dta = new DataTable();
            dta.Columns.Add("Emp_Name", typeof(string));

            dta.Columns.Add("User_Shift", typeof(string));
            dta.Columns.Add("Shift_Emp_Status", typeof(string));
            dta.Columns.Add("Shift_Date", typeof(DateTime));
            dta.Columns["Shift_Date"].AllowDBNull = true;

            dta.Rows.Add(new object[] { "John", "Morning", "abc", DateTime.Parse("1/1/15") });
            dta.Rows.Add(new object[] { "John", "Morning", "abc", DateTime.Parse("1/2/15") });
            dta.Rows.Add(new object[] { "John", "Afternoon", "abc", DateTime.Parse("1/3/15") });
            dta.Rows.Add(new object[] { "John", "Morning", "abc", DateTime.Parse("1/14/15") });
            dta.Rows.Add(new object[] { "John", "Morning", "abc", DateTime.Parse("1/15/15") });
            dta.Rows.Add(new object[] { "John", "Afternoon", "abc", DateTime.Parse("1/26/15") });
            dta.Rows.Add(new object[] { "Mary", "Morning", "abc", DateTime.Parse("1/15/15") });
            dta.Rows.Add(new object[] { "Mary", "Morning", "abc", DateTime.Parse("1/22/15") });
            dta.Rows.Add(new object[] { "Mary", "Evening", "abc", DateTime.Parse("1/23/15") });
            dta.Rows.Add(new object[] { "Mary", "Afternoon", "abc", DateTime.Parse("1/24/15") });
            dta.Rows.Add(new object[] { "Mary", "Morning", "abc", null });
            dta.Rows.Add(new object[] { "Mary", "Evening", "abc", DateTime.Parse("1/26/15") });

            DataTable displayTable = new DataTable();
            DateTime firstDayOfMonth = DateTime.Parse("1/1/15");
            int numberOfDaysInMonth = firstDayOfMonth.AddMonths(1).AddDays(-1).Day;
            DateTime dayCounter = firstDayOfMonth;

            displayTable.Columns.Add("Name", typeof(string));
            for (int i = 0; i < numberOfDaysInMonth; i++)
            {
                displayTable.Columns.Add(dayCounter.ToShortDateString(), typeof(string));
                dayCounter = dayCounter.AddDays(1);
            }


            var results = dta.AsEnumerable()
                .GroupBy(x => x.Field<string>("Emp_Name"))
                .Select(x => new
                {
                    name = x.Select(y => y.Field<string>("Emp_Name")).FirstOrDefault(),
                    shifts = x.Select(y => y.Field<string>("User_Shift")).ToList(),
                    days = x.Select(y => y.Field<object>("Shift_Date")).ToList()
                }).ToList();

            foreach (var row in results)
            {
                string[] shifts = new string[numberOfDaysInMonth + 1];
                shifts[0] = row.name;
                for (int i = 0; i < row.days.Count; i++)
                {
                    if (row.days[i] != null)
                    {
                        int dayOfMonth = ((DateTime)(row.days[i])).Day;
                        string shift = row.shifts[i];
                        shifts[dayOfMonth] = shift;
                    }

                }

                DataRow shiftRow = displayTable.Rows.Add(shifts);
            }

            dataGridView1.DataSource = displayTable;
        }
    }
}
​