Привязать DataGridviewComboBoxCell к DataGridViw - PullRequest
0 голосов
/ 05 января 2012

enter image description hereenter 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];
        }
...