动态数据集
是关于查询SQL 数据库当中一个表的。有一个表Score,它的属性是Class(班级),Course(课程),Score(成绩).在一个窗口上,有个班级的combo控件,还有一个课程的combo控件,以及一个datagridview控件。我想实现的是动态的数据集,让datagridview只输出我在combo控件中符合条件的班级和课程的成绩。我编了一段代码,可以它每次都是提醒没有找到任何数据,当然datagridview中也不能显示任何数据。下面我把代码贴出来,各位帮忙看看,我不知道哪里不对啦~~~谢谢 private DataView dataviewScore = new DataView();
private string selectStr = null;
private void btnCheck_Click(object sender, EventArgs e)
{
try
{
conn1.Open();
dataviewScore.Table = dataSetScoreCheck.Tables["Score"];//设置dataview的筛选条件
dataviewScore.RowFilter = SearchSrt_Made();
dataGridView1.DataSource = dataviewScore;
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
conn1.Close();
}
if (dataviewScore.Count == 0)
{
MessageBox.Show("没有符合条件的记录", "没有记录",
MessageBoxButtons.OK, MessageBoxIcon.Information);
comboClass.Text = "";
comboCourse.Text = "";
}
}
private string SearchSrt_Made()
{
string searchStr = null;
bool first = true;
if ( != null)
{
searchStr = "Class=" + "'" + comboClass.Text + "'";
first = false;
selectStr = "班级名称:" + comboClass.Text;
}
if ( != null)
{
if (first)
{
searchStr = "Course=" + "'" + comboCourse.Text + "'";
first = false;
selectStr = "课程名称:" + comboCourse.Text;
}
else
{
searchStr = searchStr +
" and Course=" + "'" + comboCourse.Text + "'";
selectStr += "课程名称" + comboCourse.Text;
}
}
return searchStr;
}