当前位置: 首页 > 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);

        }
    }
}

相关文章:

  • 详解 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种打印方法
  • 把中国声音带向世界,DG和Blue Note落户中国
  • 曾犯强奸罪教师出狱后办教培机构?柳州鱼峰区教育局回应
  • 玉渊谭天丨卢拉谈美国降低对华关税:中国的行动捍卫了主权
  • 7月纽约举办“上海日”,上海大剧院舞剧《白蛇》连演三场
  • 4月企业新发放贷款利率处于历史低位
  • 甘肃发布外卖食品安全违法行为典型案例:一商家用鸭肉冒充牛肉被罚