在DataGridViewComboboxColumn上设置选定的项

时间:2023-01-19 08:58:37

I have a datagridview with a DataGridViewComboboxColumn column with 3 values:

我有一个datagridview,它有一个DataGridViewComboboxColumn列,列有3个值:

"Small", "Medium", "Large"

“小”、“中等”、“大”

I get back the users default which in this case is "Medium"

返回用户默认值这里是"Medium"

I want to show a dropdown cell in the datagridview but default the value to "Medium". i would do this in a regular combobox by doing selected index or just stting the Text property of a combo box.

我想在datagridview中显示一个下拉单元格,但默认值为“Medium”。我可以在常规的combobox中这样做,方法是执行选定的索引,或者只是stting组合框的文本属性。

4 个解决方案

#1


5  

When you get into the datagridview it is probably best to get into databinding. This will take care of all of the selected index stuff you are talking about.

当您进入datagridview时,最好是进入数据库。这将处理您所讨论的所有选定的索引内容。

However, if you want to get in there by yourself,

但是,如果你想自己进去,

DataGridView.Rows[rowindex].Cells[columnindex].Value 

will let you get and set the value associated to the DataGridViewComboBoxColumn. Just make sure you supply the correct rowindex and columnindex along with setting the value to the correct type (the same type as the ValueMember property of the DataGridViewComboBoxColumn).

将让您获取并设置与DataGridViewComboBoxColumn关联的值。只需确保您提供了正确的rowindex和columnindex,并将值设置为正确的类型(与DataGridViewComboBoxColumn的ValueMember属性相同)。

#2


2  

DataGridViewComboBoxColumn ColumnPage = new DataGridViewComboBoxColumn();
ColumnPage.DefaultCellStyle.NullValue = "Medium";

#3


0  

Are you retrieving the user data and attempting to set values in the DataGridView manually, or have you actually bound the DataGridVew to a data source? Because if you've bound the grid to a data source, you should just need to set the DataPropertyName on the column to be the string name of the object Property:

您是正在检索用户数据并试图手动设置DataGridView中的值,还是已经将DataGridVew绑定到数据源?因为如果将网格绑定到数据源,那么只需将列上的DataPropertyName设置为对象属性的字符串名称:

[DataGridViewComboboxColumnName].DataPropertyName = "PropertyNameToBindTo";

Or do you mean you want it to default to Medium for a new row?

或者你的意思是你想让它默认为一个新行中的媒体?

#4


0  

Do accomplish this task you should do something like this:-

完成这个任务你应该做这样的事情:-

          this.dataGridViewStudentInformation.Columns[ColumnIndex].DataPropertyName = dataGridViewStudentInformation.Columns[2].Name ; //Set the ColumnName to which you want to bind.  

And set the default value in Database as Medium.

并将数据库中的默认值设置为介质。

#1


5  

When you get into the datagridview it is probably best to get into databinding. This will take care of all of the selected index stuff you are talking about.

当您进入datagridview时,最好是进入数据库。这将处理您所讨论的所有选定的索引内容。

However, if you want to get in there by yourself,

但是,如果你想自己进去,

DataGridView.Rows[rowindex].Cells[columnindex].Value 

will let you get and set the value associated to the DataGridViewComboBoxColumn. Just make sure you supply the correct rowindex and columnindex along with setting the value to the correct type (the same type as the ValueMember property of the DataGridViewComboBoxColumn).

将让您获取并设置与DataGridViewComboBoxColumn关联的值。只需确保您提供了正确的rowindex和columnindex,并将值设置为正确的类型(与DataGridViewComboBoxColumn的ValueMember属性相同)。

#2


2  

DataGridViewComboBoxColumn ColumnPage = new DataGridViewComboBoxColumn();
ColumnPage.DefaultCellStyle.NullValue = "Medium";

#3


0  

Are you retrieving the user data and attempting to set values in the DataGridView manually, or have you actually bound the DataGridVew to a data source? Because if you've bound the grid to a data source, you should just need to set the DataPropertyName on the column to be the string name of the object Property:

您是正在检索用户数据并试图手动设置DataGridView中的值,还是已经将DataGridVew绑定到数据源?因为如果将网格绑定到数据源,那么只需将列上的DataPropertyName设置为对象属性的字符串名称:

[DataGridViewComboboxColumnName].DataPropertyName = "PropertyNameToBindTo";

Or do you mean you want it to default to Medium for a new row?

或者你的意思是你想让它默认为一个新行中的媒体?

#4


0  

Do accomplish this task you should do something like this:-

完成这个任务你应该做这样的事情:-

          this.dataGridViewStudentInformation.Columns[ColumnIndex].DataPropertyName = dataGridViewStudentInformation.Columns[2].Name ; //Set the ColumnName to which you want to bind.  

And set the default value in Database as Medium.

并将数据库中的默认值设置为介质。