[英]Set dynamically combobox values on Datagridview
I'm trying to set a combobox value dynamically from a query with the following code and it's displayed only when I click on the combobox column, if I don't click the value disappears and it's not set. 我正在尝试使用以下代码从查询中动态设置组合框值,并且仅当我单击组合框列时才显示该组合框值,如果不单击,该值将消失并且未设置。
The first 5 values are related to the user and the one displayed on the combobox might be not set into database, so it might be null if it doesn't exist or Type "A" or "C". 前5个值与用户相关,并且组合框上显示的值可能未设置到数据库中,因此如果不存在则为空,或者键入“ A”或“ C”。
This is image is before clicking on the cell 这是单击单元格之前的图像
This image is after clicking the cell with data loaded 此图像是单击已加载数据的单元格后的
This image is after the cell is unclicked and selected any other cell 该图像是在单元格被取消单击并选择任何其他单元格之后
//This code is used to Build combobox column after the data is binded
var items = new[] { new Logica.Items { valor = "C", texto = "Creador" }, new Logica.Items { valor = "A", texto = "Aprobador" } };
DataGridViewComboBoxColumn cell = new DataGridViewComboBoxColumn();
cell.DataSource = items;
cell.ValueMember = "valor";
cell.DisplayMember = "texto";
this.dataGridView1.Columns.Insert(5, cell);
this.dataGridView1.Columns[5].HeaderText = "TipoUsuario";
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
string valor = dataGridView1[0, i].Value.ToString();//get the first datagridview value
if (valor != null || valor != "")
{
string orgv = string.Empty + this.dataGridView1[0, i].Value.ToString().ToUpper();//required to fill combobox conditions
string veg = string.Empty + this.dataGridView1[1, i].Value.ToString().ToUpper();//required to fill combobox conditions
string aprobador = string.Empty + this.dataGridView1[3, i].Value.ToString().ToUpper();//required to fill combobox conditions
DataTable ds = cargarnodos("LLenaTipoUsuario"); // this query into db to get the combobox values if this exists, if not then it's null.
foreach (DataRow datar in ds.Rows)
{
if (orgv == datar["VKORG"].ToString() && veg == datar["VTWEG"].ToString() && aprobador == datar["idAprobador"].ToString())
{
string seleccion = datar["TipoUsuario"].ToString().ToUpper();
this.dataGridView1[5, i].Value = seleccion; // this changes value
MessageBox.Show(dataGridView1[5, 0].Value.ToString());
break;
}
}
}
I managed to get the rows to display the data in the cell even if the row is selected or not. 我设法使行能够在单元格中显示数据,即使该行是否被选中也是如此。 This was done by setting the
DataPropertyName
of the ComboBoxColumn. 这是通过设置ComboBoxColumn的
DataPropertyName
来完成的。
cell.DataPropertyName = "valor"; //
This should be data column should be available in corresponding to the Datasource
of the parent DataGridView
and their data types are identical.. 这应该是与父
DataGridView
的数据Datasource
相对应的数据列,并且它们的数据类型相同。
Further if this column in the can contain null
, you add the following line (if you are allowing the user to select/change etc) 此外,如果中的此列可以包含
null
,则添加以下行(如果允许用户选择/更改等)
cell.DefaultCellStyle.NullValue = "-- Select -- ";
In my case, while loading the form is not possible to set the combobox values. 就我而言,在加载表单时无法设置组合框值。 After the datagridview is binded, it's needed to use the following command to edit values based on previous selections.
绑定datagridview后,需要使用以下命令根据先前的选择来编辑值。
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(selecciongv_DataBindingComplete); dataGridView1.DataBindingComplete + =新的DataGridViewBindingCompleteEventHandler(selecciongv_DataBindingComplete); // selecciongv is the method to do the same proccess.
// selecciongv是执行相同过程的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.