c# DataGridView的列设置combobox和设置RichTextBox

我的DataGridView单元格输入中文乱码,RichTextBox可以正常输入中文,我的思路就是RichTextBox覆盖单元格,然后把RichTextBox的值传给单元格

1.新建两个控件

    // 定义下拉列表框
    private ComboBox cmb_Temp = new ComboBox();
    //
    private RichTextBox text_Temp = new RichTextBox();

绑定性别下拉列表框

 		/// <summary>
        /// 绑定性别下拉列表框
        /// </summary>
    private void BindSex()
    {
        DataTable dtSex = new DataTable();
        dtSex.Columns.Add("Value");
        dtSex.Columns.Add("Name");
        DataRow drSex;
        drSex = dtSex.NewRow();
        drSex[0] = "0";
        drSex[1] = "男";

            dtSex.Rows.Add(drSex);
            drSex = dtSex.NewRow();
            drSex[0] = "1";
            drSex[1] = "女";

            dtSex.Rows.Add(drSex);

            cmb_Temp.ValueMember = "Value";
            cmb_Temp.DisplayMember = "Name";
            cmb_Temp.DataSource = dtSex;
            cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
        }

绑定数据

private void BindData()
        {
            //view绑定datatable
            DataTable dtData = new DataTable();
            dtData.Columns.Add("column_bool", typeof(bool));
            dtData.Columns.Add("column_text");
            dtData.Columns.Add("column_combobox");


            DataRow drData;
            drData = dtData.NewRow();
            drData[0] = 1;
            drData[1] = "test";
            drData[2] = "0";

            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 0;
            drData[1] = "test";
            drData[2] = "1";
            dtData.Rows.Add(drData);

            this.dataGridView1.DataSource = dtData;
        }
        ```

private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
}

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        dataGridView1.CurrentCell.Value = text_Temp.Text;
    }

    private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
    {
        if (this.dataGridView1.CurrentCell == null)
        {
            return;
        }

        try
        {
            if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
            {
                Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
                string varName = dataGridView1.CurrentCell.Value.ToString();

                text_Temp.Text = this.dataGridView1.CurrentCell.Value.ToString();


                text_Temp.Left = rect.Left;
                text_Temp.Top = rect.Top;
                text_Temp.Width = rect.Width;
                text_Temp.Height = rect.Height;
                text_Temp.Visible = true;
                text_Temp.Focus();
                text_Temp.Select(text_Temp.SelectionStart, 0);

            }
            else
            {
                text_Temp.Visible = false;
            }


            if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
            {
                Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
                string sexValue = dataGridView1.CurrentCell.Value.ToString();

                cmb_Temp.Text = sexValue;

                cmb_Temp.Left = rect.Left;
                cmb_Temp.Top = rect.Top;
                cmb_Temp.Width = rect.Width;
                cmb_Temp.Height = rect.Height;
                cmb_Temp.Visible = true;

                cmb_Temp.FlatStyle = FlatStyle.Popup;
            }
            else
            {
                cmb_Temp.Visible = false;
            }
        }
        catch
        {

        }
    }

源码下载地址:https://download.csdn.net/download/wojiuguowei/13747291
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页