[英]Populate DataGridView Columns only from DataTable Dynamically
I am writing a stock system and the current interface I am working on allows users to add items to the database.我正在编写一个股票系统,我正在使用的当前界面允许用户将项目添加到数据库中。 There is a different table for each item type.每个项目类型都有一个不同的表。
What I can currently do, is populate the DataGridView
with the entire table and its content, which is obviously not ideal.我目前可以做的是用整个表格及其内容填充DataGridView
,这显然不理想。
What I would like to happen is, a user selects the type of item they want to add to the database from a ComboBox
this then populates a DataGridView
with the columns from the corresponding table for that item, but not the content.我想要发生的是,用户从ComboBox
选择他们想要添加到数据库的项目类型,然后使用该项目对应表中的列填充DataGridView
,而不是内容。 The user can then add a new row and populate the fields.然后用户可以添加新行并填充字段。
here is the code behind the ComboBox
, this also allows the user to select the sub type from a second ComboBox
.这是ComboBox
背后的代码,这也允许用户从第二个ComboBox
中选择子类型。
var row = comboBox1.SelectedItem.ToString();
comboBox2.Items.Clear();
comboBox2.Text = "";
if (comboBox1.SelectedItem.ToString() == "ARRAYS")
{
var nameDT = (from names in ARRAYS.AsEnumerable()
where names.Field<string>("Array Type") != null
select names.Array_Type);
var cleanList = nameDT.Distinct().ToArray();
foreach (var item in cleanList)
{
comboBox2.Items.Add(item);
}
}
if (comboBox1.SelectedItem.ToString() == "BATT_CHARGER")
{
var nameDT = (from names in BATT_CHARGER.AsEnumerable()
where names.Field<string>("BATT_SIZE") != null
select names.BATT_SIZE);
var cleanList = nameDT.Distinct().ToArray();
foreach (var item in cleanList)
{
comboBox2.Items.Add(item);
}
}
if (comboBox1.SelectedItem.ToString() == "BUFFERBOX")
{
var nameDT = (from names in BUFFERBOX.AsEnumerable()
where names.Field<string>("BufferBox Type") != null
select names.BufferBox_Type);
var cleanList = nameDT.Distinct().ToArray();
foreach (var item in cleanList)
{
comboBox2.Items.Add(item);
}
}
How can I set the DataGridView
columns to the same as the tables they will be saving into, without filling it with all the row data?如何将DataGridView
列设置为与它们将保存到的表相同,而不用所有行数据填充它?
ARRAYS数组
BUFFERBOX缓冲箱
BATT_CHARGER电池充电器
are all table names, and Array Type, BATT_SIZE and BufferBox Type are column names.均为表名,Array Type、BATT_SIZE、BufferBox Type为列名。
I ended up doing this which works perfectly.我最终做到了这一点,效果很好。
if (comboBox1.SelectedItem.ToString() == "USB_RS232")
{
var nameDT = (from names in USB_RS232.AsEnumerable()
where names.Field<string>("MODEL") != null
select names.MODEL);
datatable = USB_RS232;
var cleanList = nameDT.Distinct().ToArray();
foreach (var item in cleanList)
{
comboBox2.Items.Add(item);
}
}
dataGridView1.Columns.Clear();
foreach (DataColumn item in datatable.Columns)
{
var colname = item.ColumnName.ToString();
dataGridView1.Columns.Add(colname, colname);
}
The dataTable
is selected when you pick from the comboBox
then i just loop throught he column names in the dataTable
and add them to the DGV.当您从组合comboBox
选择时会选择数据表,然后我只是循环遍历dataTable
表中的列名并将它们添加到dataTable
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.