将DataGridviewComboBoxCell绑定到DataGridView

时间:2012-01-05 09:42:59

标签: c# winforms datagridview datagridcomboboxcolumn datagridviewcomboboxcell

enter image description here enter image description here

我必须使用不同的数据绑定行的每个单元格,例如。

ProductID     Color(DataGridviewComboBoxColumn)

1             Red - (ComboboxCell having value only Red,Black,Green and Red is Selected)

....

4             Yellow- (ComboboxCell having value only Yellow,Gold and Yellow is Selected)

当我绑定单元格的项目并进行调试时,它将显示DataGridviewComboBoxCell中的项目,但是当要运行应用程序时,它将在选择Combobox上显示空白。

我能为此做些什么?

表单设计

private void InitializeComponent()
    {
        this.dataGridView2 = new System.Windows.Forms.DataGridView();
        this.ProductId = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Color = new System.Windows.Forms.DataGridViewComboBoxColumn();
        this.ColorIds = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView2
        // 
        this.dataGridView2.AllowUserToAddRows = false;
        this.dataGridView2.AllowUserToDeleteRows = false;
        this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.ProductId,
        this.Color,
        this.ColorIds});
        this.dataGridView2.Location = new System.Drawing.Point(12, 12);
        this.dataGridView2.Name = "dataGridView2";
        this.dataGridView2.Size = new System.Drawing.Size(704, 312);
        this.dataGridView2.TabIndex = 1;
        // 
        // ProductId
        // 
        this.ProductId.DataPropertyName = "ProductId";
        this.ProductId.HeaderText = "ProductId";
        this.ProductId.Name = "ProductId";
        // 
        // Color
        // 
        this.Color.HeaderText = "Color";
        this.Color.Name = "Color";
        this.Color.Resizable = System.Windows.Forms.DataGridViewTriState.True;
        this.Color.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
        // 
        // ColorIds
        // 
        this.ColorIds.DataPropertyName = "ColorIds";
        this.ColorIds.HeaderText = "ColorIds";
        this.ColorIds.Name = "ColorIds";
        this.ColorIds.Visible = false;

    }

代码:

dataGridView2.AutoGenerateColumns = false;
        DataTable dt = //data from Product Table
        dataGridView2.DataSource = dt;
        DataTable dtColors = //data from Color Table
        for (int i = 0; i < dataGridView2.Rows.Count; i++)
        {
            String ColorIds = dataGridView2.Rows[i].Cells["ColorIds"].Value.ToString();
            DataTable dtfiltered = dtColors.Select("ColorId IN (" + ColorIds + ")").CopyToDataTable();
            DataGridViewComboBoxCell col = (DataGridViewComboBoxCell)dataGridView2.Rows[i].Cells["Color"];
            col.Items.Clear();
            foreach (DataRow dr in dtfiltered.Rows)
            {
                col.Items.Add(new ComboBoxItem(dr["ColorId"].ToString(), dr["Color"].ToString()));
            }
            col.Value = col.Items[0];
        }

0 个答案:

没有答案