在DataGridView控件中加入ComboBox下拉列表框的实现

本文转载:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.html

 

虽然在Visual Studio中
DataGridView控件的DataGridViewComboBoxColumn可以实现下拉列表框,但这样的列会在整列中都显示下拉列表框,不太美观,而且还要用代码实现数据绑定。本文介绍一种只在当前编辑单元格中显示下拉列表框的方法,供大家参考。

  首先新建一个Windows应用程序,将主窗体重命名为MainForm,在MainForm中加入一个DataGridView控件,命名为dgv_User。如下图所示:

  打开窗体代码窗口,在代码窗口中声明一个ComboBox的控件

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

  我们要绑定的下拉列表框的功能是选择性别,添加如下绑定性别下拉列表框的方法

/// <summary>
/// 绑定性别下拉列表框
/// </summary>
private void BindSex()
{
    DataTable dtSex = new DataTable();
    dtSex.Columns.Add("Value");
    dtSex.Columns.Add("Name");
    DataRow drSex;
    drSex = dtSex.NewRow();
    drSex[0] = "1";
    drSex[1] = "男";
    dtSex.Rows.Add(drSex);
    drSex = dtSex.NewRow();
    drSex[0] = "0";
    drSex[1] = "女";
    dtSex.Rows.Add(drSex);
    cmb_Temp.ValueMember = "Value";
    cmb_Temp.DisplayMember = "Name";
    cmb_Temp.DataSource = dtSex;
    cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
}

  通常情况下我们都是从数据库中获取数据表(或者数据集),然后绑定到DataGridView中的,这里我们为了避免连接数据库,手中构造一个数据库表,代码如下:

private void BindData()
{
    DataTable dtData = new DataTable();
    dtData.Columns.Add("ID");
    dtData.Columns.Add("Name");
    dtData.Columns.Add("Sex");
    DataRow drData;
    drData = dtData.NewRow();
    drData[0] = 1;
    drData[1] = "张三";
    drData[2] = "1";
    dtData.Rows.Add(drData);
    drData = dtData.NewRow();
    drData[0] = 2;
    drData[1] = "李四";
    drData[2] = "1";
    dtData.Rows.Add(drData);
    drData = dtData.NewRow();
    drData[0] = 3;
    drData[1] = "王五";
    drData[2] = "1";
    dtData.Rows.Add(drData);
    drData = dtData.NewRow();
    drData[0] = 4;
    drData[1] = "小芳";
    drData[2] = "0";
    dtData.Rows.Add(drData);
    drData = dtData.NewRow();
    drData[0] = 5;
    drData[1] = "小娟";
    drData[2] = "0";
    dtData.Rows.Add(drData);
    drData = dtData.NewRow();
    drData[0] = 6;
    drData[1] = "赵六";
    drData[2] = "1";
    dtData.Rows.Add(drData);
    this.dgv_User.DataSource = dtData;
}

  为窗体加载事件添加如下方法,其具体功能有详细说明:

private void MainForm_Load(object sender, EventArgs e)
{
    // 绑定性别下拉列表框
    BindSex();

    //绑定数据表
    BindData();

    // 设置下拉列表框不可见
    cmb_Temp.Visible = false;

    // 添加下拉列表框事件
    cmb_Temp.SelectedIndexChanged += new EventHandler(cmb_Temp_SelectedIndexChanged);

    // 将下拉列表框加入到DataGridView控件中
    this.dgv_User.Controls.Add(cmb_Temp);
}

  当用户选择的单元格移动到性别这一列时,我们要显示下拉列表框,添加如下事件

private void dgv_User_CurrentCellChanged(object sender, EventArgs e)
{
    try
    {
        if (this.dgv_User.CurrentCell.ColumnIndex == 2)
        {
            Rectangle rect = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, false);
            string sexValue = dgv_User.CurrentCell.Value.ToString();
            if (sexValue == "1")
            {
                cmb_Temp.Text = "男";
            }
            else
            {
                cmb_Temp.Text = "女";
            }
            cmb_Temp.Left = rect.Left;
            cmb_Temp.Top = rect.Top;
            cmb_Temp.Width = rect.Width;
            cmb_Temp.Height = rect.Height;
            cmb_Temp.Visible = true;
        }
        else
        {
            cmb_Temp.Visible = false;
        }
    }
    catch
    {
    }
}

  当用户选择下拉列表框时改变DataGridView单元格的内容

private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e)
{
    if (((ComboBox)sender).Text == "男")
    {
        dgv_User.CurrentCell.Value = "男";
        dgv_User.CurrentCell.Tag = "1";
    }
    else
    {
        dgv_User.CurrentCell.Value = "女";
        dgv_User.CurrentCell.Tag = "0";
    }
}

  当滚动DataGridView或者改变DataGridView列宽时将下拉列表框设为不可见

private void dgv_User_Scroll(object sender, ScrollEventArgs e)
{
    this.cmb_Temp.Visible = false;
}

private void dgv_User_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
    this.cmb_Temp.Visible = false;
}

  绑定数据表后将性别列中的每一单元格的Value和Tag属性(Tag为值文本,Value为显示文本)

private void dgv_User_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    for (int i = 0; i < this.dgv_User.Rows.Count; i++)
    {
        if (dgv_User.Rows[i].Cells[2].Value != null && dgv_User.Rows[i].Cells[2].ColumnIndex == 2)
        {
            dgv_User.Rows[i].Cells[2].Tag = dgv_User.Rows[i].Cells[2].Value.ToString();
            if (dgv_User.Rows[i].Cells[2].Value.ToString() == "1")
            {
                dgv_User.Rows[i].Cells[2].Value = "男";
            }
            else if (dgv_User.Rows[i].Cells[2].Value.ToString() == "0")
            {
                dgv_User.Rows[i].Cells[2].Value = "女";
            }
        }
    }
}

  效果如下图所示:

  完整的代码如下:

using System;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNameSpace
{
    public partial class MainForm : Form
    {
        // 定义下拉列表框
        private ComboBox cmb_Temp = new ComboBox();

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 绑定性别下拉列表框
        /// </summary>
        private void BindSex()
        {
            DataTable dtSex = new DataTable();
            dtSex.Columns.Add("Value");
            dtSex.Columns.Add("Name");
            DataRow drSex;
            drSex = dtSex.NewRow();
            drSex[0] = "1";
            drSex[1] = "男";
            dtSex.Rows.Add(drSex);
            drSex = dtSex.NewRow();
            drSex[0] = "0";
            drSex[1] = "女";
            dtSex.Rows.Add(drSex);
            cmb_Temp.ValueMember = "Value";
            cmb_Temp.DisplayMember = "Name";
            cmb_Temp.DataSource = dtSex;
            cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 为避免连接数据库,这里手工构造数据表,实际应用中应从数据库中获取
        /// </summary>
        private void BindData()
        {
            DataTable dtData = new DataTable();
            dtData.Columns.Add("ID");
            dtData.Columns.Add("Name");
            dtData.Columns.Add("Sex");
            DataRow drData;
            drData = dtData.NewRow();
            drData[0] = 1;
            drData[1] = "张三";
            drData[2] = "1";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 2;
            drData[1] = "李四";
            drData[2] = "1";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 3;
            drData[1] = "王五";
            drData[2] = "1";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 4;
            drData[1] = "小芳";
            drData[2] = "0";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 5;
            drData[1] = "小娟";
            drData[2] = "0";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData[0] = 6;
            drData[1] = "赵六";
            drData[2] = "1";
            dtData.Rows.Add(drData);
            this.dgv_User.DataSource = dtData;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 绑定性别下拉列表框
            BindSex();

            //绑定数据表
            BindData();

            // 设置下拉列表框不可见
            cmb_Temp.Visible = false;

            // 添加下拉列表框事件
            cmb_Temp.SelectedIndexChanged += new EventHandler(cmb_Temp_SelectedIndexChanged);

            // 将下拉列表框加入到DataGridView控件中
            this.dgv_User.Controls.Add(cmb_Temp);
        }

        // 当用户移动到性别这一列时单元格显示下拉列表框
        private void dgv_User_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.dgv_User.CurrentCell.ColumnIndex == 2)
                {
                    Rectangle rect = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, false);
                    string sexValue = dgv_User.CurrentCell.Value.ToString();
                    if (sexValue == "1")
                    {
                        cmb_Temp.Text = "男";
                    }
                    else
                    {
                        cmb_Temp.Text = "女";
                    }
                    cmb_Temp.Left = rect.Left;
                    cmb_Temp.Top = rect.Top;
                    cmb_Temp.Width = rect.Width;
                    cmb_Temp.Height = rect.Height;
                    cmb_Temp.Visible = true;
                }
                else
                {
                    cmb_Temp.Visible = false;
                }
            }
            catch
            {
            }
        }

        // 当用户选择下拉列表框时改变DataGridView单元格的内容
        private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).Text == "男")
            {
                dgv_User.CurrentCell.Value = "男";
                dgv_User.CurrentCell.Tag = "1";
            }
            else
            {
                dgv_User.CurrentCell.Value = "女";
                dgv_User.CurrentCell.Tag = "0";
            }
        }

        // 滚动DataGridView时将下拉列表框设为不可见
        private void dgv_User_Scroll(object sender, ScrollEventArgs e)
        {
            this.cmb_Temp.Visible = false;
        }

        // 改变DataGridView列宽时将下拉列表框设为不可见
        private void dgv_User_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            this.cmb_Temp.Visible = false;
        }

        // 绑定数据表后将性别列中的每一单元格的Value和Tag属性(Tag为值文本,Value为显示文本)
        private void dgv_User_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < this.dgv_User.Rows.Count; i++)
            {
                if (dgv_User.Rows[i].Cells[2].Value != null && dgv_User.Rows[i].Cells[2].ColumnIndex == 2)
                {
                    dgv_User.Rows[i].Cells[2].Tag = dgv_User.Rows[i].Cells[2].Value.ToString();
                    if (dgv_User.Rows[i].Cells[2].Value.ToString() == "1")
                    {
                        dgv_User.Rows[i].Cells[2].Value = "男";
                    }
                    else if (dgv_User.Rows[i].Cells[2].Value.ToString() == "0")
                    {
                        dgv_User.Rows[i].Cells[2].Value = "女";
                    }
                }
            }
        }
    }
}

时间: 2024-10-25 19:50:17

在DataGridView控件中加入ComboBox下拉列表框的实现的相关文章

在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)

datagrid|visual|控件|下拉|下拉列表 虽然在Visual Studio中 DataGridView控件的DataGridViewComboBoxColumn可以实现下拉列表框,但这样的列会在整列中都显示下拉列表框,不太美观,而且还要用代码实现数据绑定.本文介绍一种只在当前编辑单元格中显示下拉列表框的方法,供大家参考. 首先新建一个Windows应用程序,将主窗体重命名为MainForm,在MainForm中加入一个DataGridView控件,命名为dgv_User.如下图所示:

如何于DataGridView控件中以跨数据行方式显示数据

datagrid|控件|数据|显示 一般来说,每一个字段的内容会单独显示于DataGridView控件的一个数据行中.问题是,某些字段拥有大量文字数据,我是不是能够让该字段的内容以跨数据行的方式来显示,以便在有限的画面空间中的呈现出更完整的内容呢?答案当然是肯定的. 以图表1所示的执行画面而言,「自传」字段的内容并未单独显示于一个数据行中,而是以横跨数据行的方式,显示在同笔数据列之各字段内容的下方.相关程序代码列示如下: using System;using System.Collections

实现DataGridView控件中CheckBox列的使用实例

 最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将代码贴一下 代码如下: /// <summary>         /// 实现DataGridView控件中CheckBox列的使用         /// </summary>         /// <param name="sender">&l

实现DataGridView控件中CheckBox列的使用实例_实用技巧

复制代码 代码如下: /// <summary>        /// 实现DataGridView控件中CheckBox列的使用        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void dgvTradList_Ce

c# winform-Winform中怎么设置DataGridView控件中的某个单元格为链接格式,不是整列。

问题描述 Winform中怎么设置DataGridView控件中的某个单元格为链接格式,不是整列. Winform中怎么设置DataGridView控件中的某个单元格为链接格式,不是整列.

小白请教个关于DataGridView控件中显示数据的问题。

问题描述 小白请教个关于DataGridView控件中显示数据的问题. private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server = USER-20150322KM;database = huanghe;uid = sa; pwd = 121212"); SqlDataAdapter sda = new SqlDataAdapter(&q

winfrom datagridview 控件中 如何在修改、添加和删除后刷新数据?

问题描述 winfrom datagridview 控件中 如何在修改.添加和删除后刷新数据? winfrom datagridview 如何在修改.添加和删除之后如何刷新? 解决方案 http://blog.163.com/huang_ying_lu/blog/static/2699983201052971836350/ 解决方案二: datagridview.Remove这是自动删除一行,应该还有其他属性. 解决方案三: 你是在form上直接进行datagridview修改,删除,添加操作吗

【求助】如何打印DataGridView控件中显示的内容

问题描述 如何打印DataGridView控件中显示的内容,各位大虾有没有源代码示例一下! 解决方案 解决方案二:up解决方案三:up解决方案四:据我知道是截屏打印,在此把必要的代码附上.需要从工具箱中添加PringDocument在这里命名为pdusingSystem.Drawing.Printing;publicpartialclassI03_01:Baosight.iSuperFrame.Forms.FormBase{[System.Runtime.InteropServices.DllI

在点击dataGridview 控件中的数据时,如何显示在文本框中?

问题描述 在点击dataGridview控件中的数据时,如何显示在文本框中? 解决方案 解决方案二:在cellclik事件中把只取出来传给TextBox就行了privatevoiddataGridView1_CellClick(objectsender,DataGridViewCellEventArgse){TextBox1.Text=this.dataGridView1.Rows[1].Cells[1].Value.ToString().Trim();} 解决方案三:privatevoidda