绑定来自存储过程和下拉列表的数据以填充gridview aspx

时间:2018-04-12 09:24:40

标签: asp.net

任何人都可以帮忙吗?

我有两个存储过程,每个过程都调用两个单独的结果表。

我需要创建一个下拉列表,这样当选择一个表时,它将刷新页面并使用新表结果填充网格视图...

此刻我所拥有的是连接调用页面上的一个表加载...但我需要填充到下拉列表...

public void Refreshdata()
{
    SqlConnection con = new SqlConnection(@"Server=(LocalDB)\v11.0;Database=WorkstationCells");

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "dbo.uspTargetQuantitesg120";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    cmd.Parameters.Add("@Product", SqlDbType.VarChar, 10).Value = "g120C";

    sda.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

我知道类似的问题已得到解答,但我是新手,不知道从哪里开始。

2 个答案:

答案 0 :(得分:1)

  

首先在表单中创建一个下拉列表,然后设置AutoPostBack=true

    <asp:DropDownList ID="DropDownlist1" runat="server" AutoPostBack="true" 
    Width="200px" onselectedindexchanged="DropDownlist1_Changed">
    </asp:DropDownList>
  

Refreshdata()

void Refreshdata()
 {
SqlConnection con = new SqlConnection(@"Server=(LocalDB)\v11.0;Database=WorkstationCells");

SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "dbo.uspTargetQuantitesg120";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Parameters.Add("@Product", SqlDbType.VarChar, 10).Value = "g120C";

sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
  

Void DropDown()

void DropDown()
    {
        con.Open();
         String str = "Your Query";
        SqlCommand   cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery()
        con.Close();
    }
  

Page_Load()

 if (!IsPostBack)
        {
          DropDown();
            }
        }
  

DropDownlist1_Changed()

 protected void DropDownlist1_Changed(object sender, EventArgs e)
    {
       Refresh();
    }

答案 1 :(得分:0)

请尝试将此数据绑定到ASP.Net

中GridView中的DropDownList

https://www.aspsnippets.com/Articles/How-to-bind-data-to-DropDownList-in-GridView-in-ASPNet.aspx