简体   繁体   English

C#DataGridView以编程方式添加行

[英]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. 调度员将看到一个列表(DataGridView),所有列表都在他/她的控制之下。 Each row (unit) has 7 columns. 每行(单位)有7列。

eid (text) 
roepnr (text) 
locatie (ComboBox) 
melding (text) 
telefoon (text) 
functie (text)
status (ComboBox)

The items in both ComboBoxes must be added programmatically. 必须以编程方式添加两个ComboBox中的项目。 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? 如何将这些单元添加到DataGridView并选择正确的LocatieStatus 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." 整数是与表“ locaties”和“ statussen”中的主键相对应的外键。

First I thought I could use the DataSource property to add the units to the DataGridView. 首先,我认为我可以使用DataSource属性将单元添加到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. 但是我不确定如何在ComboBox中选择正确的项目并将这些项目添加到ComboBox中。

The database is a MySql database! 该数据库是MySql数据库!

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[] ...或使用从查询返回到数据库的任何可枚举的集合值代替string[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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