Asp.net 在dropdownlist indexchanged上填充gridview

Asp.net 在dropdownlist indexchanged上填充gridview,asp.net,.net,gridview,Asp.net,.net,Gridview,我有从数据库填充数据的gridview我想在下拉列表SelectedIndex中更改选定的数据在我选择第一个索引时更改它从数据库中选择的数据在我更改选择时获得另一个数据请尝试此代码,但这是我的代码 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void B

我有从数据库填充数据的gridview我想在下拉列表SelectedIndex中更改选定的数据在我选择第一个索引时更改它从数据库中选择的数据在我更改选择时获得另一个数据请尝试此代码,但这是我的代码

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

 private void BindData()
    {
        if (ddlTguidedit.SelectedIndex==0)
        {
            string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }
        else
        {
            string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }

 protected void ddlTguidedit_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTguidedit.SelectedIndex == 0)
        {
            string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }
        else
        {
            string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }
    }

为什么它不工作???

从中删除BindData!我回来了。您正在DropDownList SelectedIndexChanged上加载网格数据。不需要使用BindData函数!我回来了。BindData函数总是加载,无论DropDownList是什么索引,它总是将索引设为0。

选择第一个索引是什么意思?您选择了DropDownList中的第一个顶部项目还是DropDownList中索引1处的项目?它们是不同的,根据您的代码判断,将返回不同的列。在dropdownlist中,我有两个选择arbic作为avlue 1,英语作为值2,我需要根据chosie arabic或englishOk从数据库中选择数据,那么,什么不起作用呢?当我选择阿拉伯语或英语时,网格仍然有相同的数据,没有发生任何变化。它仍然将默认索引检索为0