gpt4 book ai didi

c# - 如何以编程方式设置 DataGridViewComboBoxCell 值?

转载 作者:行者123 更新时间:2023-11-30 13:02:48 25 4
gpt4 key购买 nike

我在设置 DataGridViewComboBoxCell 的值时遇到问题。 datagridview 列与选项/值绑定(bind),但是当我尝试使用 dgv.Rows.Add 为 comboBoxCells 指定列值或单独设置单元格的值时,它会生成“DataGridViewComboBoxCell 值无效”错误。如果我为这些列添加一个包含空白值的行,选项将很好地显示在组合中。

我有一个对话框,它传递了一个简单对象 NDCRecord 的数组列表。

public class NDCRecord
{
public string NDCcode = "";
public string UnitQuantity = "";
public string UnitOfMeasurement = "";
public string Type = "";
public string Number = "";
}

在对话框中,数据网格以编程方式创建,然后重新填充。

public NationalDrugCodesForm(ref ArrayList ndcRecordsIn)
{
InitializeComponent();

ndcRecords = ndcRecordsIn;
SetupDataGridViewColumns();
PopulateForm();
}

设置:

private void SetupDataGridViewColumns()
{

// -----------------------------------------------------
// Add/Del column
// -----------------------------------------------------
DataGridViewButtonColumn dgvbcAddRemove = new DataGridViewButtonColumn();
dgvbcAddRemove.HeaderText = "Add";
dgvbcAddRemove.Text = "Add";
dgvbcAddRemove.Name = "Add";

DataGridViewCellStyle addButtonStyle = new DataGridViewCellStyle();
addButtonStyle.BackColor = Color.Blue;
addButtonStyle.ForeColor = Color.White;
dgvbcAddRemove.HeaderCell.Style = addButtonStyle;

dgvNDC.Columns.Add(dgvbcAddRemove);

// -----------------------------------------------------
// Additional Columns
// -----------------------------------------------------
dgvNDC.Columns.Add("NDCCode", "NDC Code");

dgvNDC.Columns.Add("UnitQuantity", "Unit Quantity");

DataGridViewComboBoxColumn unitOfMeasurement = new DataGridViewComboBoxColumn();
unitOfMeasurement.HeaderText = "Unit Of Measurement";
unitOfMeasurement.Name = "UnitOfMeasurement";
dgvNDC.Columns.Add(unitOfMeasurement);

DataGridViewComboBoxColumn type = new DataGridViewComboBoxColumn();
type.HeaderText = "Type";
type.Name = "Type";
dgvNDC.Columns.Add(type);

dgvNDC.Columns.Add("Number", "Prescription Number");

AddLine("Del", "", "", "", "", "");

BindUnitOfMeasurement((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["UnitOfMeasurement"]);
BindType((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["Type"]);
}

AddLine 函数:

    private void AddLine(string buttonLabel, string ndcCode, string unitQuantity, string unitOfMeasurement, string type, string rxNumber)
{
dgvNDC.Rows.Add(new object[] { buttonLabel, ndcCode, unitQuantity, unitOfMeasurement, type, rxNumber });
}

绑定(bind)函数:

    private void BindUnitOfMeasurement(DataGridViewComboBoxCell cb)
{
string[] Values = { "F2", "GR", "ME", "ML", "UN" };
string[] Choices = { "F2 - International Unit", "GR - Gram", "ME - Milligram", "ML - Milliliter", "UN - Unit" };

ControlManip.DataBindDDL(cb, Choices, Values);
}

private void BindType(DataGridViewComboBoxCell cb)
{
string[] Values = { "XZ", "VY" };
string[] Choices = { "XZ - Prescription Number", "VY - Link Sequence Number" };

ControlManip.DataBindDDL(cb, Choices, Values);
cb.Value = "XZ";
}
public static void DataBindDDL(ref ComboBox cb, string[] Choices, string[] Values)
{
DataTable dt = new DataTable();

dt.Columns.Add("Choice");
dt.Columns.Add("Value");

if (Choices.Length != Values.Length)
{
throw new Exception("Number of Choices and Values do not match!");
}
else
{
dt.Rows.Add(new object[] { "", "" });
for (int i = 0; i < Choices.Length; i++)
{
if (Choices[i] is object && Values[i] is object)
{
dt.Rows.Add(new object[] { Choices[i], Values[i] });
}
}

cb.DataSource = dt;
cb.DisplayMember = "Choice";
cb.ValueMember = "Value";
}
}

填写表格:

    private void PopulateForm()
{
if (ndcRecords == null || ndcRecords.Count == 0)
return;

dgvNDC.Rows.Clear();

foreach(NDCRecord record in ndcRecords)
{
AddLine("Del", record.NDCcode, record.UnitQuantity, record.UnitOfMeasurement, record.Type, record.Number);
}
}

最佳答案

问题是我绑定(bind)的是单个 CELL 而不是包含下拉框的 COLUMNS。我在单击“添加”按钮的地方插入了绑定(bind)方法,这使得它看起来好像添加一个包含空白值的行仍然会导致填充的下拉列表。但是添加具有指定值的行会导致错误,因为该行的下拉列的集合中还没有任何内容。所以这里是对代码的重要修改:

在 SetupDataGridViewColumns 而不是

    BindUnitOfMeasurement((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["UnitOfMeasurement"]);
BindType((DataGridViewComboBoxCell)dgvNDC.Rows[0].Cells["Type"]);

使用

BindUnitOfMeasurement((DataGridViewComboBoxColumn)dgvNDC.Columns["UnitOfMeasurement"]);
BindType((DataGridViewComboBoxColumn)dgvNDC.Columns["Type"]);

并适当修改绑定(bind)方法。然后,您可以为组合添加一行空白值或指定值,它会起作用(其中一个下面有一个指定的默认值,另一个留空供用户选择)

AddLine("Del", "", "", "", "XZ", "");

另外,我不确定这是否有必要,但在实验过程中我还为每个组合框列添加了这个:

type.ValueType = typeof (string);

关于c# - 如何以编程方式设置 DataGridViewComboBoxCell 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14388184/

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