问题描述
我正在做myschool项目,使用DataGridView做更新,他需要根据试卷的难易程度来显示数据集,可是我只能在当前的更新,我该怎么办??????????????usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceMySchool{publicpartialclassQuestionListForm:Form{SqlConnectioncon=null;SqlCommandcmd=null;SqlDataAdapteradp=null;DataSetdataset=newDataSet("myDB");publicQuestionListForm(){InitializeComponent();}publicenumColumnName{QuestionId,Question,Answer,Difficulty,SubjectId,OptionA,OptionB,OptionC,OptionD,};privatevoidQuestionListForm_Load(objectsender,EventArgse){try{SetupDataSource();DataGridView1.DataSource=bindingSource1;DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue=null;}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidmybind(SqlConnectioncon){stringstrselectQuestion=comboBox1.Text;stringsqlstr="select*fromQuestionwhereDifficulty='"+strselectQuestion+"'";SqlDataAdapteradp=newSqlDataAdapter(sqlstr,con);DataSetdataset=newDataSet("myDB");adp.Fill(dataset,"Question");DataGridView1.DataSource=dataset.Tables["Question"].DefaultView;}privatevoidSetupDataSource(){con=newSqlConnection(ConnectiongString.GetString());stringsql="Select*fromQuestion";cmd=newSqlCommand(sql,con);adp.Fill(dataset,"Question");newSqlCommandBuilder(adp);bindingSource1.DataSource=dataset;bindingSource1.DataMember="Question";}privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){SqlConnectioncon=newSqlConnection(ConnectiongString.GetString());mybind(con);}privatevoidbtnClose_Click(objectsender,EventArgse){this.Close();}privatevoidbtnUpdate_Click(objectsender,EventArgse){try{this.Validate();this.bindingSource1.EndEdit();this.adp.Update(dataset,"Question");}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidbutton1_Click(objectsender,EventArgse){DataGridView1.Visible=true;con=newSqlConnection(ConnectiongString.GetString());adp=newSqlDataAdapter("SelectDistinctDifficultyfromQuestion",con);dataset=newDataSet("myDB");adp.Fill(dataset,"Question");comboBox1.DisplayMember="Difficulty";comboBox1.DataSource=dataset.Tables["Question"].DefaultView;mybind(con);}privatevoidcomboBox1_SelectedValueChanged(objectsender,EventArgse){SetupDataSource();DataGridView1.DataSource=bindingSource1;DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue=null;}}}
解决方案
解决方案二:
你想实现什么?一行注释都没有,晕
解决方案三:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceMySchool{publicpartialclassQuestionListForm:Form{SqlConnectioncon=null;SqlCommandcmd=null;SqlDataAdapteradp=null;DataSetdataset=newDataSet("myDB");publicQuestionListForm(){InitializeComponent();}publicenumColumnName//枚举Question表中内容{QuestionId,Question,Answer,Difficulty,SubjectId,OptionA,OptionB,OptionC,OptionD,};privatevoidQuestionListForm_Load(objectsender,EventArgse){try{SetupDataSource();//更新表DataGridView1.DataSource=bindingSource1;DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue=null;}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidmybind(SqlConnectioncon)//绑定,利用下拉菜单出现不同的表{stringstrselectQuestion=comboBox1.Text;stringsqlstr="select*fromQuestionwhereDifficulty='"+strselectQuestion+"'";SqlDataAdapteradp=newSqlDataAdapter(sqlstr,con);DataSetdataset=newDataSet("myDB");adp.Fill(dataset,"Question");DataGridView1.DataSource=dataset.Tables["Question"].DefaultView;}privatevoidSetupDataSource(){con=newSqlConnection(ConnectiongString.GetString());stringsql="Select*fromQuestion";cmd=newSqlCommand(sql,con);adp.Fill(dataset,"Question");newSqlCommandBuilder(adp);bindingSource1.DataSource=dataset;bindingSource1.DataMember="Question";}privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){SqlConnectioncon=newSqlConnection(ConnectiongString.GetString());mybind(con);}privatevoidbtnUpdate_Click(objectsender,EventArgse)//更新表{try{this.Validate();this.bindingSource1.EndEdit();this.adp.Update(dataset,"Question");}catch(Exceptionex){MessageBox.Show(ex.Message);}}}}我想要根据下拉列表选择表,而且可以更新,但是貌似我的更新在form中写的,因此只能跟新一个当前表,以后就不能更新了,不知道在那个方法中可以多个表中都可以更新!
解决方案四:
定义dataset全局变量选择表查询数据绑定到dataset中。再根据表名实现数据publicvoidUpdate(){adp.Update(ds,"Table1");}}