Home > Net >  Datagridview combobox displaymember and valuemember inconsistent error
Datagridview combobox displaymember and valuemember inconsistent error

Time:02-20

I just started learning c # winforms, follow teacher training video winform exercise, there was a problem,
Once the datagridview combobox displaymember value member and is not consistent, is an error


This is a problem statement
String majorSql="select distinct tblmajor majorname, tblmajor. MajorID from TestClass" +
"Left the join tblmajor on TestClass. Majorid=tblmajor. Majorid";
The DataTable majorDt=Sqlhelper. Sqlhelper. ExecuteDataTable (majorSql);
DataGridViewComboBoxColumn cbomajorname=DGVStu. Columns [" majorname "] as DataGridViewComboBoxColumn;
Cbomajorname. DisplayMember="majorname";
Cbomajorname. ValueMember= "majorid ";
Cbomajorname. The DataSource=majorDt;

This is no problem statement
String majorSql="select distinct tblmajor majorname, tblmajor. MajorID from TestClass" +
"Left the join tblmajor on TestClass. Majorid=tblmajor. Majorid";
The DataTable majorDt=Sqlhelper. Sqlhelper. ExecuteDataTable (majorSql);
DataGridViewComboBoxColumn cbomajorname=DGVStu. Columns [" majorname "] as DataGridViewComboBoxColumn;
Cbomajorname. DisplayMember="majorname";
Cbomajorname. ValueMember=" majorname ";
Cbomajorname. The DataSource=majorDt;

CodePudding user response:

The fault is datagridviewcomboboxcell value is invalid

CodePudding user response:

https://blog.csdn.net/u013992365/article/details/54292338? Utm_medium=distribute. Pc_relevant. None - task - blog - BlogCommendFromMachineLearnPai2-6. Control& Depth_1 - utm_source=distribute. Pc_relevant. None - task - blog - BlogCommendFromMachineLearnPai2-6. Control

The eldest brother said is very clear,
https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.datagridviewcomboboxcolumn? Redirectedfrom=MSDN& View=.net 5.0,
With reference to the MSDN,
A typical solution is to control DataGridView binding to the parent database table, and a drop-down list is bound to the related child table, for example, you can put the DataGridView control is bound to a column contains ProductID the Orders table, and column the DataSource property is set to include columns ProductID and column ProductName Products table, in this case, you can use the "column" "ProductID" DataPropertyName attribute is set to the Orders. The ProductID to fill the column of unit value, however, if you want to in the unit and the drop-down list shows the actual product name, then you can Products by ValueMember property is set to "ProductID" and will DisplayMember attribute is set to "ProductName", will these values are mapped to table,
-- -- -- -- -- - poking fun at the official MSDN for keyword hyperlinks result in, paragraph statement is not smooth, I have done adjust -- -- -- -- -- -- -- -- -- --
Anyhow cbocol this column for datagirdview DataPropertyName binding large tables in a column name consistent
Value and the column should be with cbocol. The DataSource a column in the corresponding
And
Cbocol. ValueMember is also consistent with the above two columns
Cbocol DisplayMember just a map display, not the actual value
  •  Tags:  
  • C#
  • Related