new to C#
I have created a form , with a working coding , but i need an extra assistance.
Somewhat , i am stuck on trying to figure out how to let the 3rd combobox value , determined by the first and second.
Like this :
For the Main Category and Sub-Category , i have it working with this code.
This code also includes the 3rd ComboBox which operates without the first and second combobox.
public partial class User : Form
{
Dictionary<string, List<string>> Category = new Dictionary<string, List<string>>();
DataSet ds1;
public User()
{
InitializeComponent();
}
private void User_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn);
ds1 = new DataSet();
daMain.Fill(ds1, "Maincate");
DataTable dt = ds1.Tables["MAINCATE"];
foreach (DataRow dr in dt.Rows)
{
List<string> SubCats = new List<string>
{
dr["Subcat1"].ToString(),
dr["Subcat2"].ToString(),
dr["Subcat3"].ToString(),
dr["Subcat4"].ToString()
};
Category.Add(dr["mainCate"].ToString(), SubCats);
mainCatU.Items.Add(dr["mainCate"].ToString());
}
mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
mainCatU.Enabled = true;
subCatU.DropDownStyle = ComboBoxStyle.DropDownList;
//**Code for third combobox**
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
DataTable dt1 = new DataTable();
ListU.DataSource = dt1;
daSearch.Fill(dt1);
ListU.ValueMember = "cName";
ListU.DisplayMember = "cName";
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
//**----------------------**
conn.Close();
}
private void mainCatU_SelectedIndexChanged(object sender, EventArgs e)
{
if(Category.ContainsKey(mainCatU.SelectedItem.ToString()))
{
subCatU.DataSource = Category[mainCatU.SelectedItem.ToString()];
}
}
and the databases are as shown :
dbo.MAINCATE
MAINCATE
dbo.ComDet
ComDet
and the code for the View Selected Company
button
is :
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daS = new SqlDataAdapter("select cName, cDetails, cDetails2 from ComDet where cName = @cName", conn);
daS.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dts3 = new DataTable();
daS.Fill(dts3);
dataGridView1.DataSource = dts3.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
conn.Close();
}
so i wanted to do something like , getting the value of Main Category and Sub category to effect the list of the third combo box.
- again , right now the third combo box is coded to run without the maincategory and subcategory