是否可以将数据放在datagridview的特定列中

时间:2013-06-03 16:23:11

标签: datagridview c#-3.0

有没有办法将数据从访问表显示到某些特定列。例如。 表有以下数据 BCode TCode1 Slot1 TCode2 Slot2 TCode3 Slot Batch1 T1 10:00 T2 12:00
Batch2 T1 08:00 T2 09:00 T3 11:30 Batch3 T1 08:00 T2 10:00 T3 11:00

我的datagridview有以下列 BatchCode 08:00 08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00 12:30依此类推至20:00 我想在18:00 T2和18:00以及t3 20:00以下显示T1 意味着输出应该是这样的东西

BatchCode 08:00 08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00 12:30 B1 T1 T2 B2 T1 T2 T3 B3 T1 T2 T3

我尝试了很多方法,但没有达到输出。

            aCommand1 = new OleDbCommand("select * from weekly where bday like 'Sun'", main_connection);
        aAdapter1 = new OleDbDataAdapter(aCommand1);
        ds1 = new DataSet();
        aAdapter1.Fill(ds1, "app_info");
        int recCount = ds1.Tables[0].Rows.Count;
        dataGridView1.ColumnCount = 11;
        dataGridView1.Columns[0].Name = "Batch";
        dataGridView1.Columns[1].Name = "08:00";
        dataGridView1.Columns[2].Name = "08:30";
        dataGridView1.Columns[3].Name = "09:00";
        dataGridView1.Columns[4].Name = "09:30";
        dataGridView1.Columns[5].Name ="10:00";
        dataGridView1.Columns[6].Name ="10:30";
        dataGridView1.Columns[7].Name ="11:00";
        dataGridView1.Columns[8].Name ="11:30";
        dataGridView1.Columns[9].Name ="12:00";
        dataGridView1.Columns[10].Name ="12:30";
        for (int k = 0; k < recCount; k++)
        {
            int p_count = Convert.ToInt32(ds1.Tables[0].Rows[k][2].ToString());
            int col_count = 5;               // Column number of starting time slot
            string[] str = new string[p_count];
            int i = 0;
            for (int x = 1; x <= p_count; x++)
            {
                col_count += 2;   

                for (int m = 1; m < dataGridView1.ColumnCount; m++)
                {
                    if (dataGridView1.Columns[m].Name == ds1.Tables[0].Rows[k][col_count].ToString())
                    {
                        str[i] = ds1.Tables[0].Rows[k][col_count].ToString();
                        dataGridView1.Rows.Add(ds1.Tables[0].Rows[k][0].ToString(), str[i]);
                    }
                }
            }
        }
        dataGridView1.Columns[0].ReadOnly = true;

1 个答案:

答案 0 :(得分:0)

经过漫长的等待3小时,当我没有得到任何答复或从这里回复时我问了cbsecsnip.in并且这段代码正常工作

for (int k = 0; k < recCount; k++)
        {
            int p_count = Convert.ToInt32(ds1.Tables[0].Rows[k][2].ToString());
            int col_count = 5;
            int teat = 4;
            string[] str = new string[p_count];
            int i = 0;
            string batchname = ds1.Tables[0].Rows[k][0].ToString();
            string[] teacher=new string[dataGridView1.ColumnCount];
            dataGridView1.Rows.Add(batchname);
            for (int x = 1; x <= p_count; x++)
            {
                for (int m = 1; m < dataGridView1.ColumnCount; m++)
                {
                    string colname = dataGridView1.Columns[m].Name;
                    string tablecoltime = ds1.Tables[0].Rows[k][col_count].ToString();
                    if (colname == tablecoltime)
                    {
                        teacher[m] = ds1.Tables[0].Rows[k][teat].ToString();
                        dataGridView1.Rows[k].Cells[m].Value = ds1.Tables[0].Rows[k][teat].ToString();
                    }
                    else
                    {
                        teacher[m] = "";
                    }
                }
                teat += 2; col_count += 2;
            }
        }
        dataGridView1.Columns[0].ReadOnly = true;
    }