清除datagridview的数据

时间:2016-04-06 06:08:12

标签: c# datagridview

我有一个tabcontrol,它有9个tabpage集合,每个tabpage都有一个datagridview和一个搜索框。

 private void txtsrchesd_TextChanged(object sender, EventArgs e)
 {
    if (txtsrchesd.Text == "")
    {

    }
    else
    {
        string constring = @"Data Source=JAY\J_SQLSERVER;Initial Catalog=FillingDatabase;User ID=jay;Password=pass1234";
        string query = " SELECT * FROM esd_view  where department like '" + txtsrchesd.Text + "%' order by department ";

        SqlConnection scon = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand(query, scon);
        SqlDataReader dr;
        DataTable dt = new DataTable();
        SqlDataAdapter sql = new SqlDataAdapter(query, scon);
        sql.Fill(dt);
        sql.Dispose();

        dgesd.DataSource = dt;

        memoDatabaseDataSetBindingSource.DataSource = dt.DefaultView;

    }
}
private void txtsrchope_TextChanged(object sender, EventArgs e)
{
    if (txtsrchope.Text == "")
    {

    }
    else
    {
        string constring = @"Data Source=JAY\J_SQLSERVER;Initial Catalog=FillingDatabase;User ID=jay;Password=pass1234";
        string query = " SELECT * FROM operations_view  where department like '" + txtsrchope.Text + "%' order by department ";

        SqlConnection scon = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand(query, scon);
        SqlDataReader dr;
        DataTable dt = new DataTable();
        SqlDataAdapter sql = new SqlDataAdapter(query, scon);
        sql.Fill(dt);
        sql.Dispose();

        dgoper.DataSource = dt;

        memoDatabaseDataSetBindingSource.DataSource = dt.DefaultView;

    }
}

其他datagridview的输出显示在其他datagridview上,当我清除我在搜索框上输入的内容时,如何清除datagridview的输出

希望您理解,谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

当您查看时:

{
    if (txtsrchope.Text != "")
    {}
    .....
}

在其他部分,您不需要像以下时间一样触发相同的查询:

txtsrchop.text ==""

您可以使用以下代码替换您的其他部分:

else
{
    string constring = @"Data Source=JAY\J_SQLSERVER;Initial Catalog=FillingDatabase;User ID=jay;Password=pass1234";
        string query = " SELECT * FROM operations_view  ";

    SqlConnection scon = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand(query, scon);
    SqlDataReader dr;
    DataTable dt = new DataTable();
    SqlDataAdapter sql = new SqlDataAdapter(query, scon);
    sql.Fill(dt);
    sql.Dispose();

    dgoper.DataSource = dt;

    memoDatabaseDataSetBindingSource.DataSource = dt.DefaultView;

}