C# DataGridView控件中DataGridViewComboBoxColumn 怎么样使用下拉更改事件

勇哥注:

这个问题勇哥在网上找了许久。

假设A、B单元格都是下拉DataGridViewComboBoxColumn。

我想A单元格选中一个内容,B单元格进行刷新列表。

如果是DataGridView默认设置下,你会发现:CellEndEdit、CellClick、CellMouseDown、CellMouseUp这些都做不到完美的效果。

最多是能做到点A后,然后点击一下B单元格,才能看到刷新效果。


本文主题困难的原因是:

单元格编辑完成内容后,这个单元格仍然是“正在编辑状态”,并没有进入“单元格编辑完成状态”你得点击另一个单元格,或者按一下回车、F4(依EditMode设定)等等,才能把状态切换到“单元格编辑完成状态”

另外,如果你的单元格处于“正在编辑状态”,那么取它的value,得到的是之前的值,而不是当前你填写的值。

控件并没有一个事件是 “combox完成选择”。你得自己动手创造它。

以上这些艰难的地方也只有研究过的人才能秒懂的。。。。它还真不简单。。。



勇哥参考了网上的资料,编写了下面的代码:




源码:

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

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        ComboBox _dgv;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for(int i=0;i<6;i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1[0, i].Value = i + 1;
            }
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if(dataGridView1.CurrentCell.ColumnIndex==1 && dataGridView1.CurrentCell.RowIndex!=-1)
            {
                _dgv = e.Control as ComboBox;
                _dgv.SelectedIndexChanged -= new EventHandler(dgv_selectIndexChanged);
                _dgv.SelectedIndexChanged += new EventHandler(dgv_selectIndexChanged);
            }
        }

        private void dgv_selectIndexChanged(object sender,EventArgs e)
        {
            switch(_dgv.Text)
            {
                case "计算机01班":
                    dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = "李少华";
                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = "第1名";
                    break;
                case "计算机02班":
                    dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = "张妙音";
                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = "第2名";
                    break;
                case "计算机03班":
                    dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = "罗辑";
                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = "第3名";
                    break;
                case "计算机04班":
                    dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = "无名";
                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = "第4名";
                    break;
            }
        }

    }
}



源码下载:

支付5元或购买VIP会员后,才能查看本内容!立即支付升级会员查询订单


--------------------- 

作者:hackpig

来源:www.skcircle.com

版权声明:本文为博主原创文章,转载请附上博文链接!


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2023-04-09 22:03:53 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864