简体   繁体   中英

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.

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[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM