当前位置: 首页 > news >正文

C# Combox 绑定数据

1.在界面中添加一个combox
在这里插入图片描述

2.将数据绑定到combox

 List<GrindingType> type = new List<GrindingType>();
 type.Add(new GrindingType { Id = 1, Name = "Product A", Type = new List<string> { "1", "2" } });
 type.Add(new GrindingType { Id = 2, Name = "Product 2", Type = new List<string> { "1", "2" } });
 type.Add(new GrindingType { Id = 3, Name = "Product 3", Type = new List<string> { "1", "2" } });
 type.Add(new GrindingType { Id = 4, Name = "自定义",Type=null});

 comboBox1.DataSource = type; // 使用DataSource属性加载数据,
 comboBox1.DisplayMember = "Name"; // 设置显示成员为Name属性。
 comboBox1.ValueMember = "Id"; // 设置值成员为Id属性。

3.为combox添加列表变动事件
在这里插入图片描述
4.获取列表点击列数据

//获取comboBox的DisplayMember值
Console.WriteLine("DisplayMember:" + comboBox1.Text);
//获取comboBox的ValueMember值
Console.WriteLine("ValueMember:" + comboBox1.SelectedValue.ToString());
//获取combox上绑定数据
GrindingType grindingType = (GrindingType)comboBox1.SelectedItem;
Console.WriteLine("选中值:" + grindingType.Id + "..." + grindingType.Name);

5.完整实现

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public class GrindingType
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<string> Type { get; set; }
        }
        public Form1()
        {
            InitializeComponent();
            LoadComboBoxData();
        }

        private void LoadComboBoxData()
        {
            List<GrindingType> type = new List<GrindingType>();
            type.Add(new GrindingType { Id = 1, Name = "Product A", Type = new List<string> { "1", "2" } });
            type.Add(new GrindingType { Id = 2, Name = "Product 2", Type = new List<string> { "1", "2" } });
            type.Add(new GrindingType { Id = 3, Name = "Product 3", Type = new List<string> { "1", "2" } });
            type.Add(new GrindingType { Id = 4, Name = "自定义", Type = null });

            comboBox1.DataSource = type; // 使用DataSource属性加载数据,
            comboBox1.DisplayMember = "Name"; // 设置显示成员为Name属性。
            comboBox1.ValueMember = "Id"; // 设置值成员为Id属性。
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            Console.WriteLine("===================SelectedValueChanged=======================");

            //获取comboBox的DisplayMember值
            Console.WriteLine("DisplayMember:" + comboBox1.Text);
            //获取comboBox的ValueMember值
            Console.WriteLine("ValueMember:" + comboBox1.SelectedValue.ToString());
            //获取combox上绑定数据
            GrindingType grindingType = (GrindingType)comboBox1.SelectedItem;
            Console.WriteLine("选中值:" + grindingType.Id + "..." + grindingType.Name);

        }
    }
}

http://www.dtcms.com/a/38450.html

相关文章:

  • 详解 Spring 配置数据源的两种方式
  • 【Linux】进程控制
  • 代码审计入门学习
  • CCA社群共識聯盟正式上線
  • N皇后问题(位运算版本)
  • Tips :仿真竞争条件 指的是什么?
  • FFmpeg+vvenc实现H.266的视频编解码教程
  • SAP-ABAP:使用ST05(SQL Trace)追踪结构字段来源的步骤
  • JavaScript系列(88)--Babel 编译原理详解
  • 5秒修改文件默认打开方式-windows版
  • 蓝桥杯 Java B 组之回溯剪枝优化(N皇后、数独)
  • Milvus x DeepSeek 搭建低成本高精度 RAG 实战
  • 【部署优化篇十三】深度解析《DeepSeek API网关:Kong+Nginx配置指南》——从原理到实战的超详细手册
  • androidstudio 运行项目加载很慢,优化方法
  • Golang适配达梦数据库连接指定模式
  • 第十章:服务器消费者管理模块
  • 基于Springboot的游戏分享网站【附源码】
  • Java如何解决彻底解决,大数据量excel导出内存溢出问题
  • 【前端】页面结构管理
  • C# 打印Word文档 – 4种打印方法
  • 知识管理接入DeepSeek大模型,能够带来什么新体验?
  • 人工智能的无声基石:被低估的数据革命
  • ubuntu 安全策略(等保)
  • 最新Java面试题,常见面试题及答案汇总
  • 蓝桥杯 Java B 组之记忆化搜索(滑雪问题、斐波那契数列)
  • 深入xtquant:掌握实时行情订阅的艺术
  • 1.部署zookeeper集群:2181
  • vue自定义指令千分位
  • ssh工具
  • ROS的action通信——实现阶乘运算(一)