1
votes

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

1
I understand your issue, but the only reason I can't upvote your question, it sounds like, here is my code how to do it. Like, WTH is this ConnectionTCP.CboFetchData( ? This is not standard net functionality or is it? Still, I've provided the answer how to do it - T.S.
No problem. Glad the issue is sorted. It is just more useful to the community when questions are not specific to a business. For example, "how to fill list of different objects" vs "how to make collection of owners and pets" - T.S.
I'm sorry, I added the code you requested. Thanks! - ncode314

1 Answers

0
votes

I guess, the easiest way will be to use DataSet with related DataTables but you can create data sources that are related

// Declare combo items
public class ParentInfo
{
    int Id {get; set;}
    string Display {get; set;}
}

public class ChildInfo
{
    int Id {get; set;}
    int ParentId {get; set;}
    string Display {get; set;}
}

// Load lists
List<ParentInfo> _parents;
List<ChildInfo> _children;
// . . . 
// Set parent combo, this one is not changing
cboParents.ValueMemeber = "Id";
cboParents.DisplayMemeber = "Display";
cboParents.DataSourse = _parents;

// . .  . . . 
// Change children for different parents
void OnParent_SelectedIndexChanged( . .  . .)
{
    if (cboParents.SelectedInfex = -1)
    {
        cboChildren.DataSource = null;
        return;
    }
    cboChildren.ValueMemeber = "Id";
    cboChildren.DisplayMemeber = "Display";
    ParentInfo currentP = (ParentInfo)cboParents.SelectedItem;
    cboChildren.DataSourse = _children.Where(c => c.ParentId == currentP.Id).ToList();

}