在C#中清除DataGrid以获取新的搜索结果

时间:2018-11-18 20:14:10

标签: c# asp.net

我试图清除DataGrid以获得新的结果视图,但无法这样做。我编写这些代码或其他任何问题吗?请帮我。这是代码:

private void btnsearch_Click(object sender, EventArgs e)
    {

        string sid = txtsid.Text;
        string subject = cmbsubject.Text;
        string term = txtterm.Text;
        string year = cmbyear.Text;
        string stclass = cmbclass.Text;

        string connection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\sms.mdf;Integrated Security=True";
        string queryAll = "SELECT * FROM Result WHERE sid='"+sid+"' OR term='"+term+"' OR subject='"+subject+"' OR class='"+stclass+"' OR year = '"+year+"'";


        SqlConnection con = new SqlConnection(connection);
        con.Open();
        SqlCommand cmd = new SqlCommand(queryAll, con);

        try
        { 
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;

            DataTable dataset = new DataTable();
            sda.Fill(dataset); 

            BindingSource bs = new BindingSource();
            bs.DataSource = dataset;

            // dgResult is the changed name of the DataGridView
            dgResult.DataSource = bs;
            sda.Update(dataset);

        }
        catch (Exception ex)
        {
            MessageBox.Show(Convert.ToString(ex));
        }
        finally
        {
            con.Close();
        }
    }

0 个答案:

没有答案