例如:如果管理员可以查看网格中的所有数据。当以教员身份登录时,他/她只能在网格中查看他/她的数据,其余的必须对他们隐藏。通过更改 sql 查询,我能够根据facultyID
(主要)进行过滤,但是在显示网格中的位置时,由于 while 循环而发生了变化。建议我一个解决方案
private void FillDisGrid()
{
int a1 = 0;
int colNo = 1;
if (conn.State == ConnectionState.Open) { conn.Close(); }
conn.Open();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.Connection = conn;
cmd.CommandText = "select Subjectname,FacultyID from timetabletab where FacultyID='" + comboBox2.Text + "' and semester='" + comboBox3.Text + "' order by rno";
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (colNo % 6 == 0) { colNo = 1; a1 = a1 + 2; }
dataGridView1.Rows[a1].Cells[colNo].Value = dr.GetValue(0).ToString();
dataGridView1.Rows[a1 + 1].Cells[colNo].Value = dr.GetValue(1).ToString();
colNo++;
}
}