首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将DataGridViewComboBoxCell改回DataGridviewCell

如何将DataGridViewComboBoxCell改回DataGridviewCell
EN

Stack Overflow用户
提问于 2014-08-28 15:18:41
回答 1查看 572关注 0票数 1

我有个datagridview。

当我双击一个单元格时,它会变成DataGridViewComboBoxCell,然后我希望当用户选择一个索引时,它会像以前一样变回DataGridviewCell,并带有一个新值。

我该怎么办?

代码语言:javascript
运行
复制
namespace GridEnterChanged
{
    public partial class Form1 : Form
    {
        DataTable dt;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sqlConnection1.Open();
             dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter("select * from customer", sqlConnection1);
            sda.Fill(dt);
        dataGridView1.EditingControlShowing +=dataGridView1_EditingControlShowing;
        dataGridView1.CellEndEdit+=dataGridView1_CellEndEdit;
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "FName";
        comboBox1.ValueMember = "CustomerID";

        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.DataSource = dt;
        cmb.DisplayMember = "FName";
        cmb.ValueMember = "CustomerID";

        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 4;
        //cmb.Items.Add("True");
        //cmb.Items.Add("False");
        dataGridView1.Columns.Add(cmb);
        dataGridView1.DataSource = dt;


        sqlConnection1.Close();

        dataGridView1.DataError += dataGridView1_DataError;
    }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //throw new NotImplementedException();
        }
 private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        { DataGridView dg = sender as DataGridView;
            MessageBox.Show(dg.SelectedCells[0].ToString()+e.ColumnIndex+"  "+e.RowIndex);

            DataGridViewComboBoxCell dvc = new DataGridViewComboBoxCell();

            dvc.DataSource = dt;
        //int selectedrowindex = e.SelectedCells[0].RowIndex;
        int tmp = dg.CurrentCell.ColumnIndex;
        dvc.DisplayMember = dg.Columns[tmp].HeaderText;
        dvc.ValueMember = "CustomerID";
        this.dataGridView1[e.ColumnIndex, e.RowIndex] = dvc;

    }
EN

回答 1

Stack Overflow用户

发布于 2014-08-28 17:52:10

以此页面上的示例为例:

http://msdn.microsoft.com/en-us/library/7tas5c80%28v=vs.110%29.aspx

并用ComboBox替换DateTimePicker。

然后,当编辑控件处于活动状态时,它将只显示组合框下拉箭头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25542922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档