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

相关推荐
friklogff2 小时前
【C#生态园】虚拟现实与增强现实:C#开发库全面评估
c#·ar·vr
VB.Net3 小时前
EmguCV学习笔记 VB.Net 12.1 二维码解析
opencv·计算机视觉·c#·图像·vb.net·二维码·emgucv
白帽黑客cst4 小时前
网络安全(黑客技术) 最新三个月学习计划
网络·数据结构·windows·学习·安全·web安全·网络安全
YRr YRr5 小时前
在Windows上安装WSL2和Ubuntu 20.04以搭建C++开发环境的详细指南
c++·windows·ubuntu·wsl2
Mac分享吧6 小时前
VMware Fusion虚拟机Mac版 安装Win10系统教程
windows·macos·操作系统·vmware·软件需求·虚拟机·分享软件
虚假程序设计8 小时前
pythonnet python图像 C# .NET图像 互转
开发语言·人工智能·python·opencv·c#·.net
空白诗8 小时前
使用 nvm 管理 node 版本:如何在 macOS 和 Windows 上安装使用nvm
windows·macos·node.js·nvm
我是苏苏9 小时前
Web开发:ABP框架3——入门级别的接口增删改查实现原理
c#·web开发·abp
Zhen (Evan) Wang9 小时前
.NET 6 API + Dapper + SQL Server 2014
数据库·c#·.net
VB.Net9 小时前
EmguCV学习笔记 VB.Net 12.3 OCR
opencv·计算机视觉·c#·ocr·图像·vb.net·emgucv