Gridview - convert textboxcell to comboboxcell and

2019-09-01 13:46发布

I have a question about datagridviewcell. i have created textboxcolumn in datagridview. when ever i click the particular cell the cell should change to datagridviewcomboboxcell. Also i need to add the item in combobox collection. when i go to another cell then, the new cell get datagridviewcombobox cell and existing cell should change to datagridviewtextbox cell...

1条回答
来,给爷笑一个
2楼-- · 2019-09-01 14:33

You could change the DataGridViewTextBoxCell to DataGridViewComboBoxCell in the CellBeginEdit event and change it back in CellEndEdit event.

Try this:

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {   DataGridViewComboBoxCell cb=new DataGridViewComboBoxCell();
            cb.Items.Add(dataGridView1.CurrentCell.Value);
            //add your other itmes here;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = cb;//change the DataGridViewTextBoxCell to DataGridViewComboBoxCell 
        }

        delegate void settexthandler(DataGridViewCellEventArgs e); //use delegate to prevent reentrant call 

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.BeginInvoke(new settexthandler(settoTextBox), new object[] { e });           
        }

        void settoTextBox(DataGridViewCellEventArgs e)
        {
            DataGridViewTextBoxCell tb = new DataGridViewTextBoxCell();

            tb.Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = tb; //set it back to DataGridViewTextBoxCell with newly selected value 
        }
查看更多
登录 后发表回答