且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在由下拉列表选择的gridview中显示数据?

更新时间:2023-02-11 21:50:04

您好,

查看此示例

Hi ,
Check this Example
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DropDownList1.DataSource = GetData();
           DropDownList1.DataValueField = "orderID";
           DropDownList1.DataTextField = "OrderAmount";
           DropDownList1.DataBind();
       }
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
       {
           using (SqlCommand Cmd = new SqlCommand("select * from ODetails where orderID= @orderid", Cn))
           {
               Cn.Open();

               Cmd.Parameters.AddWithValue("@orderid", int.Parse(DropDownList1.SelectedValue));
               SqlDataReader Dr = Cmd.ExecuteReader();
               if (Dr.HasRows)
               {
                   GridView1.DataSource = Dr;
                   GridView1.DataBind();
               }
               Dr.Close();

               Cn.Close();
           }

       }
   }
   DataTable GetData()
   {
       DataTable dt = new DataTable();
       using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
       {
           using (SqlCommand Cmd = new SqlCommand("SELECT * FROM  Orders ", Cn))
           {
               Cn.Open();
               SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
               adpt.Fill(dt);
           }

       }
       return dt;
   }







<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</div>





***的问候

M.Mitwalli



Best Regards
M.Mitwalli


您好,



如果您尝试根据下拉列表绑定网格视图,那么 -



根据我的理解,在你的下拉列表中,第一个选项是默认选择的一个。另外,下拉列表的绑定应该在页面加载中发生。



尝试,

在页面加载的if(!IsPostback)中绑定你的下拉列表。



希望这会有所帮助。

请发布您的代码,以便我们帮助您找到正确的解决方案。
Hi,

If you are trying to bind your grid view based on dropdown, then-

As per my understanding, in your dropdown first option is the default selected one.Also, the binding of dropdown should be happening in pageload.

Try,
binding your dropdown in if(!IsPostback) of the pageload.

Hope this helps.
Please post your code so that we can help with the correct solution.


p rotected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

DropDownList1 .DataSource = GetData();

DropDownList1.DataValueField =orderID;

DropDownList1.DataTextField =OrderAmount;

DropDownList1.DataBind ();

}

}

protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)

{

using(SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings [testConnectionString]。ConnectionString))

{

using(SqlCommand Cmd = new SqlCommand (选择*来自ODetails,其中orderID = @orderid,Cn))

{

Cn.Open();



Cmd.Parameters.AddWithValue(@ orderid,int.Parse(DropDownList1.SelectedValue));

SqlData Reader Dr = Cmd.ExecuteReader();

if(Dr.HasRows)

{

GridView1.DataSource = Dr;

GridView1.DataBind();

}

Dr.Close();



Cn .Close();

}



}

}

DataTable GetData ()

{

DataTable dt = new DataTable();

using(SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings [testConnectionString ] .ConnectionString))

{

使用(SqlCommand Cmd = new SqlCommand(SELECT * FROM Orders,Cn))

{

Cn.Open();

SqlDataAdapter adpt = new SqlDataAdapter(Cmd);

adpt.Fill(dt);

}



}

返回dt;

}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = GetData();
DropDownList1.DataValueField = "orderID";
DropDownList1.DataTextField = "OrderAmount";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("select * from ODetails where orderID= @orderid", Cn))
{
Cn.Open();

Cmd.Parameters.AddWithValue("@orderid", int.Parse(DropDownList1.SelectedValue));
SqlDataReader Dr = Cmd.ExecuteReader();
if (Dr.HasRows)
{
GridView1.DataSource = Dr;
GridView1.DataBind();
}
Dr.Close();

Cn.Close();
}

}
}
DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("SELECT * FROM Orders ", Cn))
{
Cn.Open();
SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
adpt.Fill(dt);
}

}
return dt;
}