i have a combobox column[category] in my datagridview. the category column in datagridview shows values from a table. i want the combobox to add user defined values at run time and save it into database and show in combobox also.
i have the basic code to show data in the table to the combobox. can someone please guide me on how to accept user input and add it to database and combobox .
- void populatecategorycombobox()
- {
- using (OleDbConnection Connection = new OleDbConnection(connectionString))
- {
- Connection.Open();
- OleDbDataAdapter sqlDa = new OleDbDataAdapter("SELECT * from category", Connection);
- DataTable dtbl = new DataTable();
- sqlDa.Fill(dtbl);
- cbxcategory.ValueMember = "catid";
- cbxcategory.DisplayMember = "catname";
- DataRow topItem = dtbl.NewRow();
- topItem[0] = 0;
- topItem[1] = "-Select-";
- dtbl.Rows.InsertAt(topItem, 0);
- cbxcategory.DataSource = dtbl;
- }
- }
Rajeev PunhaniPosted Oct 9, 2019, 9:39 PM
anna johnPosted Oct 9, 2019, 2:50 PM
anna johnPosted Oct 9, 2019, 1:39 PM
, thanks for the reply. the combo has values A,B,C from database. and also i added "Add new" value into combo. so when iclick on add new it should let me type in the value. i think i am not doing it right with your code, its not letting me type in values. can you please tell me how to handle that
Rajeev PunhaniPosted Oct 9, 2019, 12:43 PM
Ahsan RazaPosted Oct 9, 2019, 12:27 PM