C# DataGridView add row programmatically
I am building a dispatchers application for the Red Cross in my region. The dispatcher will see a list (DataGridView) with all units under his/her control. Each row (unit) has 7 columns.开发者_Go百科
eid (text)
roepnr (text)
locatie (ComboBox)
melding (text)
telefoon (text)
functie (text)
status (ComboBox)
The items in both ComboBoxes must be added programmatically. The items are listed in tables in the database. This is because the dispatcher (or supervisor) must be able to add items like locations, statuses etc.
How can I add these units to the DataGridView and have the correct Locatie
and Status
be selected? Both are integer columns in the units table in the database. The integers are the foreign keys which correspond with the primary keys in the tables "locaties" and "statussen."
First I thought I could use the DataSource property to add the units to the DataGridView. But I'm not sure how I can have the correct items selected in the ComboBoxes and have the items added to the ComboBoxes.
The database is a MySql database!
Aren't you just wanting the choices in the combo box columns to be the contents of the primary key fields of the two tables? So:
string[] locaties = <SELECT primary key field from locaties>
var cbColLocaties = dataGridView1.Columns[2] as DataGridViewComboBoxColumn;
cbColLocaties.DataSource = locaties;
string[] statussen = <SELECT primary key field from statussen>
var cbColStatussen = dataGridView1.Columns[6] as DataGridViewComboBoxColumn;
cbColStatussen.DataSource = statussen;
... or use whatever enumerable collection thing comes back from a query into your database instead of a string[]
精彩评论