WinForm控件之ComboBox,DataGridView

2021-02-10 21:20

阅读:443

标签:idv   style   object   代码   GridView   var   draw   手动   table   

1.代码结构截图

技术分享图片

2.核心代码
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormComboBoxDemos
{
    /// 
    ///     WinForm程序:ComboBox和DataGridView使用方法
    ///     LDH @ 2018-3-6
    /// 
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            BindComboBox();
            BindFonts();
            BindPeople();
            BindDataGridView();
        }

        /// 
        ///     绑定DataGridView
        /// 
        private void BindDataGridView()
        {
            //  绑定数据源
            DataGridViewHelper.BindDataGridView(GetDataTable(), dataGridView1);

            //  设置样式
            DataGridViewHelper.SetStyleForDataGridView(dataGridView1, Color.CadetBlue, Color.White, Color.LightGray,
                Color.FromArgb(254, 254, 254), Color.FromArgb(233, 249, 248));
        }

        /// 
        ///     以DataTable形式绑定下拉列表
        /// 
        private void BindPeople()
        {
            //  初始化
            cmbPeople.Items.Clear();
            cmbPeople.Enabled = true;
            cmbReportFont.DropDownStyle = ComboBoxStyle.DropDownList; //  设置样式,DropDownList样式,我们不能修改文本

            cmbPeople.DataSource = GetDataTable();
            cmbPeople.DisplayMember = "Name";
            cmbPeople.ValueMember = "Id";

            //  设置默认选中项
            cmbPeople.SelectedIndex = 0;
        }

        /// 
        ///     绑定字体
        /// 
        private void BindFonts()
        {
            //  初始化
            cmbReportFont.Items.Clear();
            cmbReportFont.Enabled = true;
            cmbReportFont.DropDownStyle = ComboBoxStyle.DropDownList; //  设置样式,DropDownList样式,我们不能修改文本

            foreach (var f in FontFamily.Families)
            {
                cmbReportFont.Items.Add(f.Name);
            }

            //  默认选中项
            cmbReportFont.SelectedItem = @"微软雅黑";
        }

        /// 
        ///     绑定下拉列表
        /// 
        private void BindComboBox()
        {
            cmbConnectionString.Items.Clear();
            cmbConnectionString.Enabled = true;
            cmbConnectionString.DropDownStyle = ComboBoxStyle.DropDown; // 设置样式,DropDown样式,我们可以修改文本

            cmbConnectionString.Items.AddRange(new object[]
            {
                "Provider=MSDAORA; Data Source=ORACLE8i7; Persist Security Info=False; Integrated Security=yes",
                "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Access.mdb",
                "Data Source=.; Initial Catalog=TestDB; User ID=sa password=123456"
            });
        }

        /// 
        ///     手动创建 DataTable
        /// 
        /// 
        public static DataTable GetDataTable()
        {
            var dt = new DataTable();

            // Columns
            dt.Columns.AddRange(new[]
            {
                new DataColumn("Id", typeof (int)),
                new DataColumn("Name", typeof (string)),
                new DataColumn("Age", typeof (int)),
                new DataColumn("Address", typeof (string)),
                new DataColumn("CarNo", typeof (string)),
                new DataColumn("Remark", typeof (string))
            });

            // Data Rows
            var row = dt.NewRow();
            row["Id"] = 1;
            row["Name"] = "Jinger Yan";
            row["Age"] = 25;
            row["Address"] = "江苏南京";
            row["CarNo"] = "苏A88888";
            row["Remark"] = "A————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 2;
            row["Name"] = "Sam Lu";
            row["Age"] = 50;
            row["Address"] = "江苏无锡";
            row["CarNo"] = "苏B88888";
            row["Remark"] = "B————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 3;
            row["Name"] = "Jason Li";
            row["Age"] = 28;
            row["Address"] = "江苏徐州";
            row["CarNo"] = "苏C88888";
            row["Remark"] = "C————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 4;
            row["Name"] = "Tom Wang";
            row["Age"] = 18;
            row["Address"] = "江苏常州";
            row["CarNo"] = "苏D88888";
            row["Remark"] = "D————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 5;
            row["Name"] = "Gloden Jin";
            row["Age"] = 28;
            row["Address"] = "江苏苏州";
            row["CarNo"] = "苏E88888";
            row["Remark"] = "E————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 6;
            row["Name"] = "Jerry Zhao";
            row["Age"] = 36;
            row["Address"] = "江苏南通";
            row["CarNo"] = "苏F88888";
            row["Remark"] = "F————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 7;
            row["Name"] = "Lily Zhou";
            row["Age"] = 53;
            row["Address"] = "江苏连云港";
            row["CarNo"] = "苏G88888";
            row["Remark"] = "G————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 8;
            row["Name"] = "David Wu";
            row["Age"] = 18;
            row["Address"] = "江苏淮安";
            row["CarNo"] = "苏H88888";
            row["Remark"] = "H————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 9;
            row["Name"] = "Dick Chen";
            row["Age"] = 19;
            row["Address"] = "江苏盐城";
            row["CarNo"] = "苏J88888";
            row["Remark"] = "J————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 10;
            row["Name"] = "Sunny Yan";
            row["Age"] = 26;
            row["Address"] = "江苏扬州";
            row["CarNo"] = "苏K88888";
            row["Remark"] = "K————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 11;
            row["Name"] = "Rainy Wang";
            row["Age"] = 52;
            row["Address"] = "江苏镇江";
            row["CarNo"] = "苏L88888";
            row["Remark"] = "L————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 12;
            row["Name"] = "Scott Xu";
            row["Age"] = 69;
            row["Address"] = "江苏泰州";
            row["CarNo"] = "苏M88888";
            row["Remark"] = "M————————————————————————————————————";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Id"] = 13;
            row["Name"] = "Richard Liu";
            row["Age"] = 44;
            row["Address"] = "江苏宿迁";
            row["CarNo"] = "苏N88888";
            row["Remark"] = "N————————————————————————————————————";
            dt.Rows.Add(row);

            return dt;
        }
    }
}
3.运行效果录像

技术分享图片

WinForm控件之ComboBox,DataGridView

标签:idv   style   object   代码   GridView   var   draw   手动   table   

原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/8513767.html

上一篇:C#基础知识

下一篇:java1.8新特性


评论


亲,登录后才可以留言!