I have two combo boxes where the first one has categories that I populate from a SQL Server database. The trick is having the second combo box show only the items from the DB that are associated with the chosen category from the first combo box.
Here my SQL code:
IF @ActionType = 'FetchDataCBOCity'
BEGIN
SELECT DISTINCT ID_City, Name_City
FROM City
END
IF @ActionType = 'FetchDataCBOState'
BEGIN
SELECT ID_State, Name_State
FROM State
END
Here my C# code:
ConnectionTCP.CboFetchData(new List<string> { "FetchDataCBOCity", spName }, "ID_City", "Name_City", comboBox1);
ConnectionTCP.CboFetchData(new List<string> { "FetchDataCBOState", spName }, "ID_State", "Name_State", comboBox2);
// CBO Fetch Data in db
public static void CboFetchData( List<string> dataList, string valueMember, string displayMember, ComboBox cbo )
{
try
{
string phrase = "CBOFETCHDATA" + ">";
foreach (var data in dataList)
{
phrase += data + ">";
}
byte[] message = Encoding.ASCII.GetBytes(phrase.TrimEnd('>'));
stream.Write(message, 0, message.Length);
var buffer = getData(tcpClient);
cbo.DataSource = DataFormatter.DeserializeData(buffer);
cbo.ValueMember = valueMember;
cbo.DisplayMember = displayMember;
}
catch (Exception e)
{
MessageBox.Show("Error: " + e.Message, Client.nameApp, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Thanks
ConnectionTCP.CboFetchData(? This is not standard net functionality or is it? Still, I've provided the answer how to do it - T.S.