简体   繁体   English

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

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

I want it to show the selected value from the drop down list and show it on gridview. 我希望它显示从下拉列表中选择的值,并将其显示在gridview上。 It is supposed to query from the database using Where to indicate the selected value to show. 应该使用“ Where”指示要显示的所选值从数据库中查询。 For example, I select james from the drop down list. 例如,我从下拉列表中选择詹姆斯。 It supposes to go to the database and query james row. 它假定去数据库并查询james行。 After that the grid view is supposed to show only one value which james. 此后,网格视图应该只显示一个卡住的值。 But now I am having a problem where the grid view show every data that is available in the database. 但是现在我遇到了一个问题,其中网格视图显示了数据库中可用的每个数据。

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());
    }
}
}

You need to get the selected value using SelectedValue of dropdownlist and then query database using this value. 您需要使用下拉列表的SelectedValue获取selected value ,然后使用该值查询database

getquery = DropDownList1.SelectedValue;

Also you are using BindData method which will always select all data from database you need to seprate this method so only selected data is bind to gridview. 另外你正在使用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();

}

You are calling ExecuteNonQuey function which is used to Insert or Update data in database so it will not return any data. 您正在调用ExecuteNonQuey函数,该函数用于InsertUpdate database数据,因此它将不return任何数据。

Also when using SqlDataAdapter you don't need to explicitly call Open and Close function for opening and closing connection. 同样,在使用SqlDataAdapter时,不需要显式调用OpenClose函数来打开和关闭连接。

Change your DropDownlist_SelectedIndexChanged function to the following: 将您的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.

相关问题 如何获取转换后的布尔值以在asp.net gridview的可编辑下拉列表中显示为选定值? - How to get converted boolean value to show as selected value in the editable drop down list in asp.net gridview? 在更新时从GridView内的下拉列表中获取选定的值 - Getting a selected value from the drop down list inside a GridView on Update 在MVC中更新时,在下拉列表中显示选定的值 - Show Selected value in drop down list on updating in MVC DisplayFor() 显示从下拉列表中选择的内容 - DisplayFor() to show what was selected from a drop down list 如何基于从下拉列表中选择的项目使表单响应(显示/隐藏表单的一部分)? - How to make a form responsive (show/hide portion of the form) based on the selected items from a drop down list? 如何从下拉列表中显示确认对话框? - how to show confirmation dialog from drop down list? 如何获得一个下拉列表以在gridview中显示选定的值? - how to get a dropdownlist to show a selected value in a gridview? 选择下拉列表值时隐藏/显示一些元素 - hide/show some elements when drop-down list value selected 如何获得下拉列表选择的值 - how to get drop down list selected value 如果未从任何下拉列表中选择任何内容,则显示错误消息 - Show Error Message if Nothing is selected from any of the Drop Down List's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM