在下拉列表中,所有值都从列开始显示.只需要一个时间值
本文关键字:时间 一个 显示 开始 下拉列表 | 更新日期: 2023-09-27 17:58:54
DropDownList
一次只需要一个值。
这是我的代码:
SqlConnection con = new SqlConnection("data source=.;initial catalog=Rupesh;integrated security=true");
SqlCommand cmd;
SqlDataAdapter da;
string query;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
query = "select * from vendor";
cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr[4].ToString());
}
con.Close();
vendordetails();
}
}
private void vendordetails()
{
try
{
con.Open();
query = "select * from vendor";
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
con.Open();
query = "select * from vendor where vendor_name='" + DropDownList1.SelectedItem.ToString() + "'";
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
而不是使用
"select * from vendor"
使用类似的东西
"select [columnname] from vendor"
将[columname]替换为要显示的列的名称。
*获取数据库中所有列的所有值。
我认为不是
if (!IsPostBack)
{
query = "select * from vendor";
}
你需要
if (!IsPostBack)
{
query = "select distinct vendor_name from vendor";
}
现在我假设当您从下拉菜单中选择供应商时,您希望在网格中看到供应商的详细信息。
你似乎离不远了
将您的proc vendordetails()调整为…………..
private void vendordetails(string vendorName = "")
{
try
{
con.Open();
query = String.Format( "select * from vendor where vendor_name = ''{0}''", vendorName);
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
然后在DropDownList1_SelectedIndexChanged中只需。。。。
vendordetails(DropDownList1.SelectedItem.Text)