如何刷新gridview以加载另一个具有不同条件的表

时间:2016-06-17 04:29:59

标签: c# mysql asp.net

我无法刷新gridview以查看第二个条件下的另一个表。我用了databind();但遗憾的是它带有运行时错误,原因是我要在第一个条件中查看的另一个表与我显示的第一个表有不同的字段。以下是我的代码,希望你能帮帮我..

if (RadioButton1.Checked == true)
        {
            DateTime dtFrom = Convert.ToDateTime(TextBox1.Text); //some DateTime value, e.g. DatePicker1.Text;
            DateTime dtTo = Convert.ToDateTime(TextBox2.Text); //some DateTime value, e.g.  DatePicker1.Text
            MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=root;password=P@$$W0RD");
            MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.direct_inc_dtr where Date between '" + dtFrom.ToString("yyyy-MM-dd") + "' and '" + dtTo.ToString("yyyy-MM-dd") + "' ", mcon);

            System.Data.DataSet ds = new System.Data.DataSet();
            mcon.Open();
            mda.Fill(ds, "root");
            GridView1.DataSource = ds.Tables["root"];

            mcon.Close();

        }
        else if (RadioButton2.Checked == true)
        {

            DateTime dtFrom = Convert.ToDateTime(TextBox1.Text); //some DateTime value, e.g. DatePicker1.Text;
            DateTime dtTo = Convert.ToDateTime(TextBox2.Text); //some DateTime value, e.g.  DatePicker1.Text
            MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=root;password=P@$$W0RD");
            MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.prob_logs_out where Date between '" + dtFrom.ToString("yyyy-MM-dd") + "' and '" + dtTo.ToString("yyyy-MM-dd") + "' ", mcon);

            System.Data.DataSet ds = new System.Data.DataSet();
            mcon.Open();
            mda.Fill(ds, "root");
            GridView1.DataSource = ds.Tables["root"];

            mcon.Close();
        }
  (1st condition) dtfrom and dttime  1st table 
   - To filter Gridview by Date base on what date or cutoff user input.

  (2nd condition) Radiobutton   two other datatable
-  To be select by user what to view and filtered by date base on the Date inputed by users from the 1st condition.

提前致谢

1 个答案:

答案 0 :(得分:3)

在gridview中使用AutoGenerateColumns = "True"