从数据库创建特殊的动态数据网格

时间:2014-01-24 14:07:15

标签: c# ms-access datagrid

我正在为我们的大学(智能半自动或半自动等)编写时间表程序。

我正在写这个C#和Access Databse。

我有一个包含以下表格的数据库:  1位老师(teacher_id,teacher_name)  2-类(class_id,class_name)  3课(lesson_id,lesson_name)  4个术语(term_id,term_name)  5小时(hour_id,hour_name)  6天(day_id,day_name)  7个时间表(time_table_id,teacher_id,lesson_id,term_id,class_id_hour_id,day_id)

我为所有表格写了Crud代码,不包括时间表

我觉得这个动作最好的是datagrid

现在对于时间表格式我想要一个用户友好的UI,如下面

www.uplooder.net/img/image/47/16197657f8466c5a1b8716aee14d768b/csharp.jpg

我想在列标题(排除右上角列)显示小时

在行中(不包括右上角)显示类

在任何班级和小时的交叉点设置老师和课程

注意:

数据网格的第一行是从数据库获取的小时数 datagrid的第2列(从右到左)是从DB获取的类 3-if之前设置任何单元格显示教师姓名和课程名称

我是怎么做到的?

请帮帮我

非常感谢

1 个答案:

答案 0 :(得分:0)

我这样做

感谢我的朋友们回答所有代码的时间表

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

namespace Time_Table
{
    public partial class timetable : Form
    {
        public timetable()
        {
            InitializeComponent();
        }


        public void FillDataGrid()
        {
            string term_id = this.comboBox1.SelectedValue.ToString();
            string day_id = this.comboBox2.SelectedValue.ToString();
            string hour_id;
            string class_id;

            string query;

            DataTable result;

            for (int rowcounter = 0; rowcounter < this.dataGridViewX1.Rows.Count; rowcounter++)
            {
                for (int columncounter = 1; columncounter < this.dataGridViewX1.Columns.Count; columncounter++)
                {
                    class_id = this.dataGridViewX1.Rows[rowcounter].Cells[0].Tag.ToString();
                    hour_id = this.dataGridViewX1.Columns[columncounter].Name;

                    query = string.Format("SELECT * FROM timetable WHERE time_table_term_id={0} " +
                        "AND time_table_day_id={1} AND time_table_hour_id={2} AND time_table_class_id={3}", term_id, day_id, hour_id, class_id);

                    result = CrudClass.SelectAll(query, "timetable");

                    if (result.Rows.Count > 0)
                    {
                        query = string.Format("SELECT teacher_name,lesson_name FROM timetable,teachers,lessons WHERE time_table_term_id={0} " +
                        "AND time_table_day_id={1} AND time_table_hour_id={2} AND time_table_class_id={3} AND time_table_teacher_id = teacher_id AND time_table_lesson_id=lesson_id"
                        , term_id, day_id, hour_id, class_id);

                        result = CrudClass.SelectByQuery(query);

                        this.dataGridViewX1.Rows[rowcounter].Cells[columncounter].Value = result.Rows[0].ItemArray[1].ToString() + " / "
                                                                                          + result.Rows[0].ItemArray[0].ToString();

                        this.dataGridViewX1.Rows[rowcounter].Cells[columncounter].Style.BackColor = Color.GreenYellow;
                    }
                    else {
                        this.dataGridViewX1.Rows[rowcounter].Cells[columncounter].Style.BackColor = Color.Red;
                    }

                }
            }
        }


        private void timetable_Load(object sender, EventArgs e)
        {
            DataTable ClassNames ;
            DataTable HourNames ;

            ClassNames = CrudClass.SelectAll("SELECT * FROM classes", "classes");
            HourNames = CrudClass.SelectAll("SELECT * FROM hours", "hours");


            this.dataGridViewX1.Columns.Add("empty", "ساعت/نام کلاس");

            for (int i = 0; i < HourNames.Rows.Count; i++)
            {
                dataGridViewX1.Columns.Add(new DataGridViewTextBoxColumn() { Name=HourNames.Rows[i].ItemArray[0].ToString(),HeaderText=HourNames.Rows[i].ItemArray[2].ToString() });
            }


            for (int classCounter = 0; classCounter < ClassNames.Rows.Count; classCounter++)
            {
                this.dataGridViewX1.Rows.Add(new object[]{ClassNames.Rows[classCounter].ItemArray[2].ToString()});
                this.dataGridViewX1.Rows[classCounter].Cells[0].Tag = ClassNames.Rows[classCounter].ItemArray[0].ToString();
            }


            //------------------------ fill combobox ------------------------------

            DataTable termValues = CrudClass.SelectAll("SELECT * FROM terms","terms");
            DataTable dayValues = CrudClass.SelectAll("SELECT * FROM days", "days");

            this.comboBox1.DataSource = termValues;
            this.comboBox1.DisplayMember = "term_name";
            this.comboBox1.ValueMember = "term_id";


            this.comboBox2.DataSource = dayValues;
            this.comboBox2.DisplayMember = "day_name";
            this.comboBox2.ValueMember = "day_id";


        }

        private void dataGridViewX1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (comboBox1.Items.Count>0 && comboBox2.Items.Count>0 && dataGridViewX1.Columns.Count>1 && dataGridViewX1.Rows.Count>0 

                && e.ColumnIndex >0 && e.RowIndex>=0 )
            {
                add_timetable frm_add_timetable = new add_timetable();
                frm_add_timetable.term_id = this.comboBox1.SelectedValue.ToString();
                frm_add_timetable.day_id = this.comboBox2.SelectedValue.ToString();
                frm_add_timetable.hour_id = this.dataGridViewX1.Columns[e.ColumnIndex].Name;
                frm_add_timetable.class_id = this.dataGridViewX1.Rows[e.RowIndex].Cells[0].Tag.ToString();
                frm_add_timetable.ShowDialog();
            }





        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FillDataGrid();
        }

        private void timetable_Activated(object sender, EventArgs e)
        {
            FillDataGrid();
        }
    }
}