dataGridView 嵌套ComboBox对单元格精准绑定数据

1,数据准备并绑定数据

复制代码
           List<P> list = new List<P>();
            for (int i = 0; i < 3; i++)
            {
                P data = new P();
                data.Idx = i + 1;
                data.Name = "名称" + i;
                list.Add(data);
            }
            dataGridView1.DataSource = list;
            dataGridView1.Refresh();

2,对单元格进行ComboBox绑定数据

复制代码
dicComSocuerType.Clear();
            Dictionary<int, string> a1= new Dictionary<int, string>();
            a1.Add(0, "一年级A");
            a1.Add(1, "一年级B");
            dicComSocuerType.Add(0, a1);
            Dictionary<int, string> a2 = new Dictionary<int, string>();
            a2.Add(0, "二年级A");
            a2.Add(1, "二年级B");
            dicComSocuerType.Add(1, a2);
            Dictionary<int, string> a3 = new Dictionary<int, string>();
            a3.Add(0, "三年级A");
            a3.Add(1, "三年级B");
            dicComSocuerType.Add(2, a3);
            if (dataGridView1.Rows.Count > 0)
            {
                foreach (KeyValuePair<int, Dictionary<int, string>> dicItem in dicComSocuerType)
                {
                    DataGridViewComboBoxCell boxCell = dataGridView1.Rows[dicItem.Key].Cells[2] as DataGridViewComboBoxCell;
                    boxCell.DataSource = null;
                    boxCell.Items.Clear();
                    boxCell.DataSource = dicItem.Value.ToList();
                    boxCell.DisplayMember = "Value";
                    boxCell.ValueMember = "Value";
                    boxCell.Value = dicItem.Value.ToList().FirstOrDefault().Value;
                }
            }

3,监听下拉修改动作并提交

复制代码
//监听这个动作发现改变后提交修改
        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
            {
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }
        }
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2&& e.RowIndex>=0)
            {
                DataGridViewComboBoxCell boxCellType = dataGridView1.Rows[e.RowIndex].Cells[2] as DataGridViewComboBoxCell;
                string Key = boxCellType.Value.ToString(); //value
                string selectedValue = boxCellType.FormattedValue.ToString(); //displayVAlue
            }
        }

最后看效果

贴一下全部代码吧

复制代码
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 WindowsFormsApp2
{
    public partial class Form3 : Form
    {
        Dictionary<int, Dictionary<int, string>> dicComSocuerType = new Dictionary<int, Dictionary<int, string>>();
        public Form3()
        {
            InitializeComponent();
            this.dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
            List<P> list = new List<P>();
            for (int i = 0; i < 3; i++)
            {
                P data = new P();
                data.Idx = i + 1;
                data.Name = "名称" + i;
                list.Add(data);
            }
            dataGridView1.DataSource = list;
            dataGridView1.Refresh();

        }
        private void Form3_Load(object sender, EventArgs e)
        {
            dicComSocuerType.Clear();
            Dictionary<int, string> a1= new Dictionary<int, string>();
            a1.Add(0, "一年级A");
            a1.Add(1, "一年级B");
            dicComSocuerType.Add(0, a1);
            Dictionary<int, string> a2 = new Dictionary<int, string>();
            a2.Add(0, "二年级A");
            a2.Add(1, "二年级B");
            dicComSocuerType.Add(1, a2);
            Dictionary<int, string> a3 = new Dictionary<int, string>();
            a3.Add(0, "三年级A");
            a3.Add(1, "三年级B");
            dicComSocuerType.Add(2, a3);
            if (dataGridView1.Rows.Count > 0)
            {
                foreach (KeyValuePair<int, Dictionary<int, string>> dicItem in dicComSocuerType)
                {
                    DataGridViewComboBoxCell boxCell = dataGridView1.Rows[dicItem.Key].Cells[2] as DataGridViewComboBoxCell;
                    boxCell.DataSource = null;
                    boxCell.Items.Clear();
                    boxCell.DataSource = dicItem.Value.ToList();
                    boxCell.DisplayMember = "Value";
                    boxCell.ValueMember = "Value";
                    boxCell.Value = dicItem.Value.ToList().FirstOrDefault().Value;
                }
            }
        }
        //监听这个动作发现改变后提交修改
        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
            {
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }
        }
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2&& e.RowIndex>=0)
            {
                DataGridViewComboBoxCell boxCellType = dataGridView1.Rows[e.RowIndex].Cells[2] as DataGridViewComboBoxCell;
                string Key = boxCellType.Value.ToString(); //value
                string selectedValue = boxCellType.FormattedValue.ToString(); //displayVAlue
            }
        }
    }
    class P
    {
        public int Idx { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
    }
}

说一下会遇到的问题

1,就是

dataGridView1.DataSource = list;

dataGridView1.Refresh();

如果也放在load事件里面不起效果

2,如果数据是一样的话是拿不到value值得,即使我valueNumber 是key dispalynumber是value

如果我两个value一样,就是key不一样,选择第二个得时候也拿不到第二个key

DataGridViewComboBoxCell boxCellType = dataGridView1.Rows[e.RowIndex].Cells[2] as DataGridViewComboBoxCell;

string Key = boxCellType.Value.ToString(); //value

string selectedValue = boxCellType.FormattedValue.ToString(); //displayVAlue

相关推荐
陈苏同学3 小时前
[已解决] VS Code / Cursor / Trae 的 PowerShell 终端 conda activate 进不去环境的常见问题
linux·windows·conda
辰%3 小时前
如何重启pycharm中的项目?
windows·python·pycharm
iangyu5 小时前
【windows server脚本每天从网络盘复制到本地】
开发语言·windows·php
FAREWELL000756 小时前
Unity基础学习(九)输入系统全解析:鼠标、键盘与轴控制
学习·unity·c#·游戏引擎
love530love7 小时前
家用或办公 Windows 电脑玩人工智能开源项目配备核显的必要性(含 NPU 及显卡类型补充)
人工智能·windows·python·开源·电脑
码观天工7 小时前
【.NET必读】RabbitMQ 4.0+重大变更!C#开发者必须掌握的6大升级要点
c#·rabbitmq·.net·mq
绿龙术士9 小时前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
o0向阳而生0o10 小时前
43、Server.UrlEncode、HttpUtility.UrlDecode的区别?
c#·.net
敲代码的 蜡笔小新10 小时前
【行为型之策略模式】游戏开发实战——Unity灵活算法架构的核心实现策略
unity·设计模式·c#·策略模式
Kookoos10 小时前
【实战】基于 ABP vNext 构建高可用 S7 协议采集平台(西门子 PLC 通信全流程)
后端·物联网·c#·.net