如何在单个gridview中显示两个查询结果?

时间:2015-03-23 08:02:57

标签: c# gridview

我试图在单个gridview中显示两个查询结果。我在网上冲浪了很多,但我得不到合适的答案。请帮助任何人。我的示例查询

    int shift=0;
    int month = Convert.ToInt32( ddmonth.SelectedItem.Value);// should be in the format of Jan, Feb, Mar, Apr, etc...
    int yearofMonth = Convert.ToInt32(ddyear.Text);
    DateTime dateTime = Convert.ToDateTime(month + "-" + "01-" + yearofMonth);
    string month1 = dateTime.Month.ToString();
    int year = Convert.ToInt32(ddyear.Text);
    DataRow dr;
    DataTable dt = new DataTable();
    dt.Columns.Add("Sunday");
    dt.Columns.Add("Monday");
    dt.Columns.Add("Tuesday");
    dt.Columns.Add("Wednesday");
    dt.Columns.Add("Thursday");
    dt.Columns.Add("Friday");
    dt.Columns.Add("Saturday");
    dr = dt.NewRow();


    for (int i = 0; i < DateTime.DaysInMonth(year, month); i += 1)
    {
        qry = "select s.Shiftname from ShiftType s,ShiftAllocation a where s.ShiftID=a.ShiftID";
        empdt = conn.gettable(qry);
        string data = empdt.Rows[shift][0].ToString();
        empdt.Columns.Add(data);
        row = empdt.NewRow();
        if (Convert.ToDateTime(dateTime.AddDays(i)).ToString("dddd") == "Sunday")
        {
            dr["Sunday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
        }
        if (Convert.ToDateTime(dateTime.AddDays(i)).ToString("dddd") == "Monday")
        {
            dr["Monday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Tuesday")
        {
            dr["Tuesday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Wednesday")
        {
            dr["Wednesday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();

        }
        if (dateTime.AddDays(i).ToString("dddd") == "Thursday")
        {
            dr["Thursday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Friday")
        {
            dr["Friday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
        }
        if (dateTime.AddDays(i).ToString("dddd") == "Saturday")
        {
            dr["Saturday"] = i + 1;
            row[data] = empdt.Rows[shift][0].ToString();
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            empdt.Rows.Add((row.ItemArray));
            row = empdt.NewRow();
            continue;
        }
        if (i == DateTime.DaysInMonth(dateTime.Year, dateTime.Month) - 1)
        {
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            empdt.Rows.Add((row.ItemArray));
            row = empdt.NewRow();
        }

    }
    GridView1.DataSource = dt;
    GridView1.DataBind();

这是我尝试创建事件日历的实际代码。我的预期o / p就像      enter image description here

现在GridView只显示dt值,我想将查询结果empdt显示为事件

2 个答案:

答案 0 :(得分:1)

string qry="select Name,Age from table Sample1 inner join table Sample2 on table Sample1.CustomerID = table Sample2.CustomerID";

dr1=con.query(qry);
GridView1 DataSource=dr1;
GridView1 DataBind();

两个表必须具有相同的列名称&#34; CustomerID&#34;。然后你可以使用这个查询。

答案 1 :(得分:0)

使用Join关键字连接两个表,然后将数据绑定到GridView。