繁体   English   中英

如何从下拉列表中显示所选值并将其显示在gridview中

[英]How to show the selected value from the drop down list and show it in gridview

我希望它显示从下拉列表中选择的值,并将其显示在gridview上。 应该使用“ Where”指示要显示的所选值从数据库中查询。 例如,我从下拉列表中选择詹姆斯。 它假定去数据库并查询james行。 此后,网格视图应该只显示一个卡住的值。 但是现在我遇到了一个问题,其中网格视图显示了数据库中可用的每个数据。

public partial class Search_Engine : System.Web.UI.Page
{
#region Database

static string HostName = "localhost";
static string DatabaseName = "finalproject";
static string TableName = "truckinfo";
//static string TableBucket = "bucketbrigade";
static string UserName = "root";
static string Password = "";

//--- Used for access to database infomation-----
string ConnStr = "Data Source=" + HostName + ";" +
                 "Database=" + DatabaseName + ";" +
                 "User ID=" + UserName + ";" +
                 "Password=" + Password;

string Qry = "";

MySqlConnection Con;
MySqlCommand Cmd;
MySqlDataReader Rdr;

#endregion
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        BindData();


        using (Con = new MySqlConnection(ConnStr))
        {
            Con.Open();
            using (Cmd = new MySqlCommand("SELECT * FROM truckinfo", Con))
            {

                using (Rdr = Cmd.ExecuteReader())
                {
                    if (Rdr.HasRows)
                    {
                        DropDownList1.DataSource = Rdr;
                        DropDownList1.DataValueField = "truckplateno";
                        DropDownList1.DataTextField = "truckplateno";
                        DropDownList1.DataBind();
                    }
                }
            }
        }
    }
}
private void BindData()
{
    DataTable dt = new DataTable();
    try
    {
        MySqlConnection Con = new MySqlConnection(ConnStr);
        Con.Open();
        MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM " +
                          DatabaseName + "." + TableName , Con);


        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        Con.Close();

    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}



protected void TextBox1_TextChanged(object sender, EventArgs e)
{




}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Con = new MySqlConnection(ConnStr);
    Con.Open();
    try
    {
        String getquery;
        // String a;
        getquery = DropDownList1.Text;
        TextBox1.Text = getquery;
        // a = TextBox2.Text;
        //  TextBox1.Text = a;
        Qry = @"SELECT * FROM finalproject.truckinfo WHERE truckplateno=" + "'" + getquery + "'" + ";";
        Cmd = new MySqlCommand(Qry, Con);
        Cmd.ExecuteNonQuery();
        Con.Close();
        BindData();
    }
        catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}
}

您需要使用下拉列表的SelectedValue获取selected value ,然后使用该值查询database

getquery = DropDownList1.SelectedValue;

另外你正在使用BindData方法将总是select all从数据database ,你需要seprate此方法,以便只有selected data被绑定到GridView控件。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
String getquery;
getquery = DropDownList1.Text;
MySqlConnection Con = new MySqlConnection(ConnStr);
Con.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM finalproject.truckinfo WHERE truckplateno='" + getquery + "'", Con);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Con.Close();

}

您正在调用ExecuteNonQuey函数,该函数用于InsertUpdate database数据,因此它将不return任何数据。

同样,在使用SqlDataAdapter时,不需要显式调用OpenClose函数来打开和关闭连接。

将您的DropDownlist_SelectedIndexChanged函数更改为以下内容:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Con = new MySqlConnection(ConnStr);
DataTable dt = new DataTable();
try
{
    Con.Open();
    string getquery = DropDownList1.SelectedValue;
    TextBox1.Text = getquery;
    // a = TextBox2.Text;
    //  TextBox1.Text = a;
    Qry = @"SELECT * FROM finalproject.truckinfo WHERE truckplateno=" + "'" + getquery + "'" + ";";
    MySqlCommand ddlCMD = new MySqlCommand(Qry, Con);
    MySqlDataAdapter msda = new MySqlDataAdapter(ddlCMD);
    msda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
    catch (Exception ex)
{
    Response.Write(ex.ToString());
}
finally{
    Con.Close();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM