![](/img/trans.png)
[英]C# Windows Forms how to change values of second combo box based on selection in first combo box
[英]Enable second combo box if the the first combo box value is scheduled in C#
當天的問候!
我是DOT NET的新手。 開發工具,構建UI。
我被困在選擇其他組合框時生成組合框值。
問題是:有2個組合框:DropDownList1和DropDownList2 DropDownList1值:0-全部1-未計划2-拒絕3-選擇的DropDownList2值:0-選擇全部1-差的溝通3-測試失敗4鍵入技巧差
值正在組合框中填充。
//對於DropDownList1
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test_DB;User ID=sa");
con.Open();
//Interview Status
string Sql = "select Id,Status from dbo.InterviewStatus";
SqlCommand cmd = new SqlCommand(Sql, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Status";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("All", "0"));
}
現在,僅當拒絕DropDownList1時,才應啟用DropDownList2。 對於其他DropDownList1值,應該禁用DropDownList2。
請幫助我解決這個問題。
提前致謝 :)
如果我對您的理解正確,則可以使用SelectedIndexChanged
事件並檢查cboStatus
的值
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.ToString() == "2")
{
DropDownList2.Enabled = true;
}
}
編輯:
由於索引似乎與您的Status
值一致,因此您實際上只需要索引,除非它對用戶是重要信息。 要填充ComboBox
只需從數據庫中以List
獲取值,然后使用AddRange
方法
DropDownList1.Items.AddRange(listOfValues.ToArray());
您可以按照以下給定的方法進行級聯樣式下拉列表,也可以在初始化過程中填充cboComments DropDownList
,因為數據很小,並且它是aspx控件,因此通常在Page_Load
事件中執行此操作可以查看該線程以獲取更多信息。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CascadingDDL._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>Cascading Drop Down List</h1>
</div>
<div class="row">
<div class="col-md-12">
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboStatus_SelectedIndexChanged">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="1" Text="Not Scheduled"></asp:ListItem>
<asp:ListItem Value="2" Text="Rejected"></asp:ListItem>
<asp:ListItem Value="3" Text="Selected"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="cboComments" runat="server">
<asp:ListItem Value="0" Text="Select All"></asp:ListItem>
<asp:ListItem Value="1" Text="Poor Communication"></asp:ListItem>
<asp:ListItem Value="3" Text="Failed in Test"></asp:ListItem>
<asp:ListItem Value="4" Text="Poor Typing skill"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</asp:Content>
背后的代碼
namespace CascadingDDL
{
using System;
using System.Web.UI;
public partial class _Default : Page
{
public const int StatusRejected = 2;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.cboComments.Enabled = false;
}
}
protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
{
var status = int.Parse(this.cboStatus.SelectedValue);
if (status == StatusRejected)
{
this.cboComments.Enabled = true;
}
else
{
this.cboComments.Enabled = false;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.