I have 2 table in my sql database:
1. city - id - cityname
2.Student -id -studentname -cityID
I successfully insert, edit and delete data in the database.
I have two c# windows form for City:
1.frmCity with dataGridView and all City data.... 2.frmAddEditCity with textbox for insert and edit....
I have two c# windows form for Student:
1.frmCity with dataGridView and all Studentdata.... 2.frmAddEditStudent with textbox for insert and edit...
BUT.....
In form frmAddEditStudent FOR CityID I have textbox and I use DataBindigs (BindigSource property) to get data... I can disply CITY id or City name...
My problem is when I want to insert new record or edit some record... and I don't know how to use combobox instead textbox "txtCityID" and save and edit record.
If I edit some record in combobox should be selected city name from that Student record(new york) with the possibility of changing through combox (and in combo are all citys from database)
is the same with insert - i need combobox with city names and value(id), and chois city from combo to save record...
This is my code for inser new record and edit record frmAddEditStudent:
- public partial class frmAddEditStudent : Form
- {
- tblStudentEntities dbIme;
- CityEntities Citydb;
- public frmAddEditStudent(tblStudent obj)
- {
- InitializeComponent();
- dbIme = new tblStudentEntities();
- if (obj == null)
- {
- tblStudentBindingSource.DataSource = new tblStudent();
-
- dbIme.tblStudents.Add(tblStudentBindingSource.Current as tblStudent);
-
- }
- else
- {
- tblStudentBindingSource.DataSource = obj;
-
- dbIme.tblStudents.Attach(tblStudentBindingSource.Current as tblStudent);
- }
- }
- private void frmAddEditStudent_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (DialogResult == DialogResult.OK)
- {
- if (string.IsNullOrEmpty(txtStudentName.Text))
- {
- MessageBox.Show("Please Insert data", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
- txtStudentName.Focus();
- e.Cancel = true;
- return;
- }
- dbIme.SaveChanges();
- e.Cancel = false;
- }
- e.Cancel = false;
- }
- private void frmAddEditStudent_Load(object sender, EventArgs e)
- {
- Citydb = new CityEntities();
- tblCityBindingSource.DataSource = Citydb.tblCitys.ToList();
- }
- }