如题

解决方案 »

  1.   

    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[0].Cells[0];
    cb.Value = "2";
      

  2.   

    动态生成ComboBox
    private ComboBox combo = null;
    private Panel p = null;private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 1)
                {
                    dataGridView1.BeginEdit(true);
                }
            }        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 1)
                {
                    try
                    {                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = combo.Text;                    if (p != null)
                        {
                            p.Hide();
                            p = null;
                        }
                    }
                    catch
                    {
                        
                    }
                    finally
                    {
                    }
                }
            }        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
            {
                if (e.ColumnIndex == 1)
                {
                    int row = e.RowIndex;
                    int column = e.ColumnIndex;                Rectangle r = dataGridView1.GetCellDisplayRectangle(column, row, false);                if (p == null)
                    {
                        p = new Panel();
                        p.BackColor = Color.White;
                        p.Parent = dataGridView1;
                        //combo.KeyPress += new KeyPressEventHandler(combo_KeyPress);
                        if (dataGridView1.Rows[row].Cells[column].Value != null)
                        {
                            combo.Text = dataGridView1.Rows[row].Cells[column].Value.ToString();
                        }
                        else
                        {
                            combo.Text = "";
                        }
                        combo.Dock = DockStyle.Top;
                        combo.Parent = p;
                    }                p.Width = r.Width - 1;
                    p.Height = r.Height;
                    p.Top = r.Top;
                    p.Left = r.Left;                p.Show();
                }
            }        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
            {
                if (p != null)
                {
                    p.Hide();
                }
            }