当前位置:编程学习 > C#/ASP.NET >>

有关DateGridView更新的

我正在做myschool项目,使用DataGridView做更新,他需要根据试卷的难易程度来显示数据集,可是我只能在当前的更新,我该怎么办??????????????
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MySchool
{
    public partial class QuestionListForm : Form
    {
        SqlConnection con = null;
        SqlCommand cmd = null;
        SqlDataAdapter adp = null;
        DataSet dataset = new DataSet("myDB");
        public QuestionListForm()
        {
            InitializeComponent();
        }
        public enum ColumnName
        {
            QuestionId,
            Question,
            Answer,
            Difficulty,
            SubjectId,
            OptionA,
            OptionB,
            OptionC,
            OptionD,

        };
        private void QuestionListForm_Load(object sender, EventArgs e)
        {
            try
            {
                               SetupDataSource();
                DataGridView1.DataSource = bindingSource1;
                DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void mybind(SqlConnection con)
        {
            string strselectQuestion = comboBox1.Text;
            string sqlstr = "select *from Question where Difficulty='" + strselectQuestion + "'";
             SqlDataAdapter adp = new SqlDataAdapter(sqlstr,con);
           
                        DataSet dataset = new DataSet("myDB");
            adp.Fill(dataset, "Question");
            DataGridView1.DataSource = dataset.Tables["Question"].DefaultView;

           
        }
   
        private void SetupDataSource()
        {
            con = new SqlConnection(ConnectiongString.GetString());
                       string sql = "Select * from Question";
            cmd = new SqlCommand(sql,con);

            

           
                        adp.Fill(dataset, "Question");
            new SqlCommandBuilder(adp);
            bindingSource1.DataSource = dataset;
            bindingSource1.DataMember = "Question";
         
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            SqlConnection con = new SqlConnection(ConnectiongString.GetString());
            mybind(con);
            

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                
                this.Validate();
                this.bindingSource1.EndEdit();
                                this.adp.Update(dataset, "Question");
                             
              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }

        
       

       
        private void button1_Click(object sender, EventArgs e)
        {
            DataGridView1.Visible = true;
            con = new SqlConnection(ConnectiongString.GetString());
            adp = new SqlDataAdapter("Select Distinct Difficulty from Question", con);
            dataset = new DataSet("myDB");
            adp.Fill(dataset, "Question");
            comboBox1.DisplayMember = "Difficulty";
            comboBox1.DataSource = dataset.Tables["Question"].DefaultView;
            mybind(con);


        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            SetupDataSource();
            DataGridView1.DataSource = bindingSource1;
            DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue = null;
        }
    }
} --------------------编程问答-------------------- 你想实现什么?一行注释都没有,晕 --------------------编程问答-------------------- using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace MySchool 

    public partial class QuestionListForm : Form 
    { 
        SqlConnection con = null; 
        SqlCommand cmd = null; 
        SqlDataAdapter adp = null; 
        DataSet dataset = new DataSet("myDB"); 
        public QuestionListForm() 
        { 
            InitializeComponent(); 
        } 
        public enum ColumnName           //枚举Question表中内容
        { 
            QuestionId, 
            Question, 
            Answer, 
            Difficulty, 
            SubjectId, 
            OptionA, 
            OptionB, 
            OptionC, 
            OptionD, 

        }; 
        private void QuestionListForm_Load(object sender, EventArgs e) 
        { 
            try 
            { 
                SetupDataSource();                                  //更新表
                DataGridView1.DataSource = bindingSource1; 
                DataGridView1.Columns[ColumnName.Answer.ToString()].DefaultCellStyle.NullValue = null; 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
        private void mybind(SqlConnection con)                  //绑定,利用下拉菜单出现不同的表
        { 
            string strselectQuestion = comboBox1.Text; 
            string sqlstr = "select *from Question where Difficulty='" + strselectQuestion + "'"; 
            SqlDataAdapter adp = new SqlDataAdapter(sqlstr,con); 
          
                        DataSet dataset = new DataSet("myDB"); 
            adp.Fill(dataset, "Question"); 
            DataGridView1.DataSource = dataset.Tables["Question"].DefaultView; 

          
        } 
  
        private void SetupDataSource() 
        { 
            con = new SqlConnection(ConnectiongString.GetString()); 
                      string sql = "Select * from Question"; 
            cmd = new SqlCommand(sql,con); 

            

          
                        adp.Fill(dataset, "Question"); 
            new SqlCommandBuilder(adp); 
            bindingSource1.DataSource = dataset; 
            bindingSource1.DataMember = "Question"; 
        
        } 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
          
            SqlConnection con = new SqlConnection(ConnectiongString.GetString()); 
            mybind(con); 
            

        } 

      
        private void btnUpdate_Click(object sender, EventArgs e)        //更新表
        { 
            try 
            { 
                
                this.Validate(); 
                this.bindingSource1.EndEdit(); 
                this.adp.Update(dataset, "Question"); 
                            
              
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
          
        } 
       
        } 
}




我想要根据下拉列表选择表,而且可以更新,但是貌似我的更新在form中写的,因此只能跟新一个当前表,以后就不能更新了,
不知道在那个方法中可以多个表中都可以更新! --------------------编程问答-------------------- 定义dataset全局变量
选择表查询数据绑定到dataset中。再根据表名实现数据
public void Update() 

adp.Update(ds, "Table1"); 


补充:.NET技术 ,  .NET Framework
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,