很简单,就是3个表格,分别显示个人信息、各科目成绩、可选课程。
一个TabControl容器里加了3个tabPage,中间那个黑框是DataGridView,每个tabPage各有一个DataGridView。设置TabControl的Dock属性为Fill,就会自动充满窗口。由于显示的内容不多,窗口和DataGridView的大小自己调整。
DataGridView设置如下:
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace SSMS
{
public partial class frmStudent : Form
{
frmLogin myLoginForm;
public frmStudent(frmLogin afrmLogin = null)
{
myLoginForm = afrmLogin;
InitializeComponent();
}
private void frmStudent_FormClosing(object sender, FormClosingEventArgs e)
{
if (myLoginForm != null)
{
myLoginForm.Close();
}
}
private void frmStudent_Load(object sender, System.EventArgs e)
{
string sqlStrPersonalInfo = @"select * from {0} where {1}='{2}'";
sqlStrPersonalInfo = string.Format(sqlStrPersonalInfo, Data.t_student, Data.c_student_snum, Data.loginID);
string sqlStrScore = @"select * from {0} where {1}='{2}'";
sqlStrScore = string.Format(sqlStrScore, Data.t_sc, Data.c_sc_snum, Data.loginID);
SqlDataAdapter sda = new SqlDataAdapter(sqlStrPersonalInfo,Data.connectionString);
DataSet ds = new DataSet();
sda.Fill(ds, "table1");
sda = new SqlDataAdapter(sqlStrScore,Data.connectionString);
sda.Fill(ds, "table2");
dataGridView1.DataSource = ds.Tables["table1"];
dataGridView2.DataSource = ds.Tables["table2"];
}
}
}
SqlDataAdapter: using System.Data.SqlClient;
DataSet: using System.Data;
给frmStudent绑定frmStudent_Load事件,这个事件是在窗口加载的时候做的事情。此后事件绑定操作无需再叙述,相信大家都已经掌握了:
运行一下,用一个学生的账号密码登录一下,101吧,先看看显示效果:
看起来不错,但是我们想把个人信息表格的列名显示成学号,姓名,班级,而且个人成绩只想显示课程名称和成绩。这就涉及到了多表查询了,需要有一定的数据库SQL知识基础。修改如下:
private void frmStudent_Load(object sender, System.EventArgs e)
{
string sqlStrPersonalInfo = @"select * from {0} where {1}='{2}'";
sqlStrPersonalInfo = string.Format(sqlStrPersonalInfo, Data.t_student, Data.c_student_snum, Data.loginID);
string sc_cnum = Data.t_sc + "." + Data.c_sc_cnum;
string course_cnum = Data.t_course + "." + Data.c_course_cnum;
string sqlStrScore = @"select {0},{1} from {2},{3} where {4}='{5}' and {6}={7}";
sqlStrScore = string.Format(sqlStrScore,
Data.c_course_cname, Data.c_sc_score,
Data.t_sc, Data.t_course,
Data.c_sc_snum, Data.loginID,
sc_cnum, course_cnum
);
SqlDataAdapter sda = new SqlDataAdapter(sqlStrPersonalInfo,Data.connectionString);
DataSet ds = new DataSet();
sda.Fill(ds, "table1");
sda = new SqlDataAdapter(sqlStrScore,Data.connectionString);
sda.Fill(ds, "table2");
dataGridView1.DataSource = ds.Tables["table1"];
dataGridView2.DataSource = ds.Tables["table2"];
dataGridView1.Columns[Data.c_student_snum].HeaderText = "学号";
dataGridView1.Columns[Data.c_student_sname].HeaderText = "姓名";
dataGridView1.Columns[Data.c_student_sclass].HeaderText = "班级";
dataGridView2.Columns[Data.c_course_cname].HeaderText = "课程名称";
dataGridView2.Columns[Data.c_sc_score].HeaderText = "分数";
}
似乎好了,但是新的问题又来了,个人成绩这个表格由于少了一列又显示不太美观了,别担心,办法总是有的:
1、Data.cs中columnAutoAdapter函数就是解决这个问题的
///
/// 自动调整列宽,以适应表格大小
///
/// 需要被调整的DataGridView对象
public static void columnAutoAdapter(DataGridView dv)
{
int totalWidth = 0;
for (int i = 0; i < dv.Columns.Count; i++)
{
totalWidth += dv.Columns[i].Width;//记录整个DataGridView的宽度
}
for (int i = 0; i < dv.Columns.Count; i++)
{
dv.Columns[i].FillWeight = (dv.Columns[i].Width * 100) / totalWidth;
}
dv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
2、frmStudent.cs加两句方法调用:
private void frmStudent_Load(object sender, System.EventArgs e)
{
...
dataGridView2.Columns[Data.c_course_cname].HeaderText = "课程名称";
dataGridView2.Columns[Data.c_sc_score].HeaderText = "分数";
Data.columnAutoAdapter(dataGridView1);
Data.columnAutoAdapter(dataGridView2);
}
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
namespace SSMS
{
public partial class frmStudent : Form
{
frmLogin myLoginForm;
const int CASEINFO = 1, CASESCORE = 2, CASEENROLL = 3;
DataTable dtInfo, dtScore, dtEnroll;
ArrayList selectAll;
public frmStudent(frmLogin afrmLogin = null)
{
myLoginForm = afrmLogin;
InitializeComponent();
}
private void frmStudent_FormClosing(object sender, FormClosingEventArgs e)
{
if (myLoginForm != null)
{
myLoginForm.Close();
}
}
void freshTable(int cas)
{
string selectStr = (string)selectAll[cas];
switch (cas)
{
case CASEINFO:
dtInfo.Clear();
Data.fillDataTable(selectStr, dtInfo);
dgvInfo.DataSource = dtInfo;
dgvInfo.Columns[Data.c_student_snum].HeaderText = "学号";
dgvInfo.Columns[Data.c_student_sname].HeaderText = "姓名";
dgvInfo.Columns[Data.c_student_sclass].HeaderText = "班级";
Data.columnAutoAdapter(dgvInfo);
break;
case CASESCORE:
dtScore.Clear();
Data.fillDataTable(selectStr, dtScore);
dgvScore.DataSource = dtScore;
dgvScore.Columns[Data.c_course_cname].HeaderText = "课程名称";
dgvScore.Columns[Data.c_sc_score].HeaderText = "分数";
Data.columnAutoAdapter(dgvScore);
break;
case CASEENROLL:
dtEnroll.Clear();
Data.fillDataTable(selectStr, dtEnroll);
dgvEnroll.DataSource = dtEnroll;
for (int i = 1; i < dgvEnroll.Columns.Count; ++i)
{
dgvEnroll.Columns[i].ReadOnly = true;
}
Data.columnAutoAdapter(dgvEnroll);
break;
default: break;
}
}
private void frmStudent_Load(object sender, System.EventArgs e)
{
dtInfo = new DataTable();
dtScore = new DataTable();
dtEnroll = new DataTable();
string sqlStrPersonalInfo = @"select * from {0} where {1}='{2}'";
sqlStrPersonalInfo = string.Format(sqlStrPersonalInfo, Data.t_student, Data.c_student_snum, Data.loginID);
string sc_cnum = Data.t_sc + "." + Data.c_sc_cnum;
string course_cnum = Data.t_course + "." + Data.c_course_cnum;
string sqlStrSelectedCourse = @"select {0},{1} from {2},{3} where {4}='{5}' and {6}={7}";
sqlStrSelectedCourse = string.Format(sqlStrSelectedCourse,
Data.c_course_cname, Data.c_sc_score,
Data.t_sc, Data.t_course,
Data.c_sc_snum, Data.loginID,
sc_cnum, course_cnum
);
string sqlStrToBeSelectedCourse = @"select * from {0} B where B.{1} not in ( select A.{2} from {3} A where A.{4} = '{5}' )";
sqlStrToBeSelectedCourse = string.Format(sqlStrToBeSelectedCourse, Data.t_course, Data.c_course_cnum, Data.c_sc_cnum, Data.t_sc, Data.c_sc_snum, Data.loginID);
selectAll = new ArrayList();
selectAll.Add("");
selectAll.Add(sqlStrPersonalInfo);
selectAll.Add(sqlStrSelectedCourse);
selectAll.Add(sqlStrToBeSelectedCourse);
freshTable(CASEINFO);
freshTable(CASESCORE);
string sqlStr = @"select * from {0}";
sqlStr = string.Format(sqlStr, Data.t_enroll);
DataTable dt = new DataTable();
Data.fillDataTable(sqlStr, dt);
if (dt.Rows[0][0].ToString().Equals("True"))
{
DataGridViewCheckBoxColumn dc = new DataGridViewCheckBoxColumn();
dc.Name = "colSelect";
dgvEnroll.Columns.Add(dc);
freshTable(CASEENROLL);
}
else
{
tabControl1.TabPages.Remove(tabPage3);
}
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < dgvEnroll.Rows.Count; ++i)
{
if (dgvEnroll.Rows[i].Cells["colSelect"].EditedFormattedValue.ToString() == "True")
{
string sqlStr = @"insert into {0} values('{1}', '{2}', NULL)";
sqlStr = string.Format(sqlStr, Data.t_sc, Data.loginID, dgvEnroll.Rows[i].Cells[Data.c_sc_cnum].Value.ToString());
SqlConnection con = new SqlConnection(Data.connectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sqlStr, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("选课成功!");
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
freshTable(CASEENROLL);
}
private void button1_Click(object sender, System.EventArgs e)
{
freshTable(CASESCORE);
}
}
}
源代码文件下载地址