【问题标题】:Is it possible to put data in particular column of datagridview是否可以将数据放在 datagridview 的特定列中
【发布时间】:2013-06-04 03:51:29
【问题描述】:

有没有办法将访问表中的数据显示到某些特定列。例如。 表有以下数据 BCode TCode1 Slot1 TCode2 Slot2 TCode3 Slot 批次1 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 有以下列 批号 08:00 08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00 12:30 以此类推直到 20:00 我想在 16:00 下显示 T1 在 18:00 下显示 T2 在 20:00 下显示 t3 意味着输出应该是这样的

批号 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;

【问题讨论】:

    标签: datagridview c#-3.0


    【解决方案1】:

    长时间等待 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2017-02-28
      • 1970-01-01
      • 2010-10-25
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多