gpt4 book ai didi

c# - 如何从数据集中设置 DataGridViewComboBoxColumn 值

转载 作者:行者123 更新时间:2023-11-30 12:32:51 26 4
gpt4 key购买 nike

假设我有两个表:

 work_hours
work_hours_id | date | _project_id
1 1.2. 10
2 1.2. 11
3 1.2. 10

project
project_id | project_name
10 pr1
11 pr2
12 pr3

在 DataGridView 中我想看到这个:

 work_hours_id | date | _person_id | project_name(DataGridViewComboBoxColumn)
1 1.2. 10 pr1
2 1.2. 11 pr2
3 1.2. 10 pr1

1. 我该怎么做?2. 如果我将 pr1 (work_hours_id = 3) 更改为 pr3 (DataGridViewComboBoxColumn) 与 SqlCommandBuilder

 string query = "SELECT work_hours.work_hours_id, work_hours.date FROM work_hours
LEFT OUTER JOIN project ON work_hours._project_id = project.project_id ORDER BY work_hours.date;
SELECT * FROM project ORDER BY project_name";

SqlCommand sqlcmd = new SqlCommand(query, conn);
da = new SqlDataAdapter(query, connectionString);
cBuilder = new SqlCommandBuilder(da);
dt = new DataTable();
ds = new DataSet();
da.Fill(dt);
da.Fill(ds);

DataGridViewComboBoxColumn columnCb = new DataGridViewComboBoxColumn();
columnCb.DataPropertyName = "_project_id";

columnCb.DataSource = ds.Tables[1];
columnCb.ValueMember = "project_id";
columnCb.DisplayMember = "project_name";

bSource = new BindingSource();
bSource.DataSource = dt;
dataGridView1.DataSource = bSource;
dataGridView1.Columns.Add(columnCb);

Here is example:

最佳答案

如果我没有误解你想设置 datagridview 的组合框列的数据源..你可以这样设置:

/// if you use OfType Method then you will get advantage of to use as normal control
/// means this method will give you advantage of can use all method and properties
/// of the control which you want to implement in DataGridView

/// Controls.OfType method will check all the columns and get an array with
/// what you give as search criteria..here, the criteria is ComboboxColumn..
/// depends of your need you can give comboboxcell also..

/// Element at method selects the zero based index in array which filtered by criteria
/// if you use only one of the given type then you can use .First() instead of .ElementAt()


yourDataGridViewName.Controls.OfType<DataGridViewComboBoxColumn>()
.ElementAt(indexNoOfTheDGVComboBox).DataSource = YourDataRetrievingMethod;
//i.e. ds.Tables[indexNoOfTheTable].Columns[IndexOfTheColumn]

/// and you can set DisplayMember and ValueMember as the same way..
/// i give combocell and combocolumn together to show the syntax.

yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).DisplayMember = "YourDisplay";

yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).ValueMember = "YourValue";

关于第二个问题..如果你问我那么我更喜欢:为了控制“意外节省”在行的末尾放置一个buttonCell..当用户更改行上的内容时应该点击保存按钮..然后在您的按钮单击方法可以将更改保存为正常的插入或更新方法..

那里只有 2 个不同之处..

1-) 您需要使用 Cell_Click 事件,并确保使用 OfType() 方法而不是普通的 Button _Click 来检查按钮> 事件

2-) 根据项目需要,您需要先获取 DataGridView 的 RowIndex 和/或 Cell Address() 才能获取值并将值传递给 SQLQuery

关于c# - 如何从数据集中设置 DataGridViewComboBoxColumn 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com