Seryu Seryu - 5 months ago 8
SQL Question

How to refresh gridview to load another table having diffrent condition

I am having trouble to refresh my gridview to view another table from 2nd condition. I used databind(); But unfortunately it comes with runtime error, the reason is the other table that I was going to view in 1st condition has different fields from the first table I displayed. Below is my code, I hope you can help me..

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.


Thanks in advance

Answer

Use AutoGenerateColumns = "True" in gridview