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(); //valuestring 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(); //valuestring 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/189214.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

248: vue+openlayers 以静态图片作为底图,并在上面绘制矢量多边形

第248个 点击查看专栏目录 本示例是演示如何在vue+openlayers项目中以静态图片作为底图,并在上面绘制矢量多边形。这里主要通过pixels的坐标作为投射,将静态图片作为底图,然后通过正常的方式在地图上显示多边形。注意的是左下角为[0,0]。 直接复制下面的 vue+openlayers源代…

获取用户详细信息

pojo.user&#xff1a;JsonIgnore注解作用忽略密码属性&#xff0c;返回给用户的信息不能有敏感属性密码 package com.lin.springboot01.pojo;import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data;import java.time.LocalDateTime;Data public class Use…

vim模式用法总结

0.前言 我们用gcc编译文件的时候&#xff0c;如果发生了下面的错误&#xff0c;那么如何用vim打开的时候就定位到&#xff1f; 我们可以知道&#xff0c;这是第6行出现了错误&#xff1b; 所以我们使用vim打开的时候多输入个这个&#xff0c;我们就可以快速定位了 vim test.c 6…

【观察】华为:数智世界“一触即达”,应对数智化转型“千变万化”

毫无疑问&#xff0c;数智化既是这个时代前进所趋&#xff0c;也是国家战略所指&#xff0c;更是所有企业未来发展进程中达成的高度共识。 但也要看到&#xff0c;由于大量新兴技术的出现&#xff0c;技术热点不停的轮转&#xff0c;加上市场环境的快速变化&#xff0c;让数智化…

使用vant list实现订单列表,支持下拉加载更多

在公司项目开发时&#xff0c;有一个需求是实现可以分页的订单列表&#xff0c;由于是移动端项目&#xff0c;所以最好的解决方法是做下拉加载更多。 1.在页面中使用vant组件 <van-listv-model"loading":finished"finished"finished-text"没有更…

Android 当中的 Fragment 协作解耦方式

Android 当中的 Fragment 协作解耦方式 文章目录 Android 当中的 Fragment 协作解耦方式第一章 前言介绍第01节 遇到的问题第02节 绘图说明 第二章 核心代码第01节 代理人接口第02节 中间人 Activity第03节 开发者A第04节 开发者B第05节 测试类 第一章 前言介绍 第01节 遇到的…

使用 React Flow 构建一个思维导图应用

思维导图是围绕共同主题或问题将思想、概念、信息或任务分组的视觉表示。思维导图应用是一种软件应用&#xff0c;允许您创建、可视化和组织您的思想、想法和信息作为思维导图。本文将向您展示如何实现自己的思维导图应用程序。 在我们开始之前&#xff0c;我想向您展示一下我们…

JAXB的XmlElement注解

依赖 如果基于JAX-WS开发&#xff0c;可以在maven工程的pom.xml文件中增加如下依赖&#xff0c;会将依赖的JAXB库也下载下来&#xff1a; <dependency><groupId>jakarta.xml.ws</groupId><artifactId>jakarta.xml.ws-api</artifactId><vers…

Android 屏幕适配

目录 一、为什么要适配 二、几个重要的概念 2.1 屏幕尺寸 2.2 屏幕分辨率 2.3 屏幕像素密度 2.4 屏幕尺寸、分辨率、像素密度三者关系 三、常用单位 3.1 密度无关像素(dp) 3.2 独立比例像素&#xff08;sp&#xff09; 3.3 dp与px的转换 四、解决方案 4.1 今日头条…

在QGIS中加载显示3DTiles数据

“我们最近有机会在QGIS 3.34中实现一个非常令人兴奋的功能–能够以“Cesium 3D Tiles”格式加载和查看3D内容&#xff01;” ——QGIS官方的 宣传介绍。 体验一下&#xff0c;感觉就是如芒刺背、如坐针毡、如鲠在喉。 除非我电脑硬件有问题&#xff0c;要么QGIS的3Dtiles是真…

Springboot更新用户头像

人们通常(为徒省事)把一个包含了修改后userName的完整userInfo对象传给后端&#xff0c;做完整更新。但仔细想想&#xff0c;这种做法感觉有点二&#xff0c;而且浪费带宽。 于是patch诞生&#xff0c;只传一个userName到指定资源去&#xff0c;表示该请求是一个局部更新&#…

wpf devexpress Property Gird管理集合属性

Property Grid允许你添加&#xff0c;浏览和编辑集合属性