簡體   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