0
我試圖在數據網格視圖表中,在運行時間組合框中添加一個值。首先,我已將項目添加到組合框中。在運行時,我試圖將數據從數據表添加到數據網格。如果數據不是組合框項目的一部分,則不接受。我希望它接受該值並將其顯示在網格中,但它不是組合框的一部分。請參閱下面的代碼示例:在運行時向DataGrid組合框添加值。該值不應該是組合框列表的一部分
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("FieldName");
dt.Columns.Add("Value");
dt.Columns.Add("Flags");
dt.Rows.Add("Field1", "Value1", "#A");
dt.Rows.Add("Field2", "Value2", "#B");
dt.Rows.Add("Field3", "Value3", "#C");
dt.Rows.Add("Field4", "Value4", "#D");
dt.Rows.Add("Field5", "Value5", "#E");
foreach (DataRow dr in dt.Rows)
{
string fieldName = Convert.ToString(dr["FieldName"]);
string value = Convert.ToString(dr["Value"].ToString());
string flags = Convert.ToString(dr["Flags"].ToString());
dataGridView1.Rows.Add(fieldName, value, flags);
}
ComboBox cb = new ComboBox();
AddListComboBox(ref cb);
((DataGridViewComboBoxColumn)dataGridView1.Columns["Flags"]).DataSource = cb.Items;
}
DataTable csvDataTable = new DataTable();
public DataTable defaultFlagsDataTable()
{
//Create default flags datatable
if (csvDataTable.Rows.Count <= 0)
{
csvDataTable.Columns.Add("Name", typeof(string));
csvDataTable.Columns.Add("Description", typeof(string));
csvDataTable.Columns.Add("Obsolete", typeof(bool));
csvDataTable.Columns.Add("Color", typeof(string));
}
//Add default flags to datatable
csvDataTable.Rows.Add("#A", "Asset Data", false, 13168840);
csvDataTable.Rows.Add("#B", "System Default", false, 15780518);
csvDataTable.Rows.Add("#C", "Data From GeoPlan", false, 65280);
csvDataTable.Rows.Add("#D", "Model Import", false, 3981040);
csvDataTable.Rows.Add("#E", "System Calculated", false, 3981040);
//csvDataTable.Rows.Add("#F", "CSV Import", false, 33023);
return csvDataTable;
}
public void AddListComboBox(ref ComboBox CB)
{
defaultFlagsDataTable();
foreach (DataRow dr in csvDataTable.Rows)
{
string flags = dr["Name"].ToString();
CB.Items.Add(flags);
}
}
}
}
有沒有人知道這個解決方案 – Raghu