首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView循环显示有关问题

2012-06-20 
GridView循环显示问题?一个GridView,一个DropDownList,一个Button ,DropDownList为(0,最近7天),(1,最近15

GridView循环显示问题?
一个GridView,一个DropDownList,一个Button ,
DropDownList为(0,最近7天),(1,最近15天),(2,最近30天),(3,查询所有)
当button点击时,根据DropDownList的SelectedIndex值查询。
该怎么循环查询好些呢。

C# code
    protected void btn_cha_Click(object sender, EventArgs e)    {        if (ddl7.SelectedIndex == 0)        {            GridView1.DataSource = null;            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";            SqlConnection con = new SqlConnection(conString);            con.Open();            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader sdr = cmd.ExecuteReader();            GridView1.DataSource = sdr;            GridView1.DataBind();            con.Close();            sdr.Close();        }        if (ddl7.SelectedIndex == 1)        {            GridView1.DataSource = null;            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";            SqlConnection con = new SqlConnection(conString);            con.Open();            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader sdr = cmd.ExecuteReader();            GridView1.DataSource = sdr;            GridView1.DataBind();            con.Close();            sdr.Close();        }        if (ddl7.SelectedIndex == 2)        {            GridView1.DataSource = null;            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";            SqlConnection con = new SqlConnection(conString);            con.Open();            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader sdr = cmd.ExecuteReader();            GridView1.DataSource = sdr;            GridView1.DataBind();            con.Close();            sdr.Close();        }        if (ddl7.SelectedIndex == 3)        {            GridView1.DataSource = null;            string sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";            SqlConnection con = new SqlConnection(conString);            con.Open();            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader sdr = cmd.ExecuteReader();            GridView1.DataSource = sdr;            GridView1.DataBind();            con.Close();            sdr.Close();        }    }


[解决办法]
别的不说,如下
C# code
private void ExecSql(string sql){            GridView1.DataSource = null;            SqlConnection con = new SqlConnection(conString);            con.Open();            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader sdr = cmd.ExecuteReader();            GridView1.DataSource = sdr;            GridView1.DataBind();            con.Close();            sdr.Close();}    protected void btn_cha_Click(object sender, EventArgs e)    {            string sql = "";        if (ddl7.SelectedIndex == 0)        {            sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";        }        if (ddl7.SelectedIndex == 1)        {           sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";        }        if (ddl7.SelectedIndex == 2)        {            sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";        }        if (ddl7.SelectedIndex == 3)        {            sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";        }        ExecSql(sql);    } 

热点排行