110,534
社区成员
发帖
与我相关
我的任务
分享
var typeColumn = new DataGridViewComboBoxColumn{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "Type",
DataPropertyName = "Type"}
If I do the following, the right types are selected, but I can't change the selection in the combo box:
var typeColumn = new DataGridViewComboBoxColumn{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "TypeID"}
If I use the following I get a FormatException error as it's trying to populate:
var typeColumn = new DataGridViewComboBoxColumn{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "Type"}
edit: typeList is a simple DataTable populated by the following:
SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]FROM InsuranceType ITWHERE IT.ClientID = @ClientIDORDER BY [Type]
回答:
Ok, I came up with an example ClientInfo and uranceDetails that I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate the DataGridViewComboBox and set the value based on the InsuranceDetails specifically at: InsurDetailz = all_insurance_types[2])
public partial class Form1 : Form
{
private ClientInfo _myClient;
private BindingList<InsuranceDetails> all_insurance_types =
new BindingList<InsuranceDetails>();
public Form1()
{
InitializeComponent();
DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;
grid.AutoGenerateColumns = true;
all_insurance_types.Add(new InsuranceDetails(1, "Health"));
all_insurance_types.Add(new InsuranceDetails(2, "Home"));
all_insurance_types.Add(new InsuranceDetails(3, "Life"));
var col = new DataGridViewComboBoxColumn
{
DataSource = all_insurance_types,
HeaderText = "Insurance Type",
DataPropertyName = "InsurDetailz",
DisplayMember = "ItType",
ValueMember = "Self",
};
_myClient = new ClientInfo {
InsurDetailz = all_insurance_types[2], Name = "Jimbo" };
grid.Columns.Add(col);
grid.DataSource = new BindingList<ClientInfo> { _myClient };
this.Controls.Add(grid);
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}