• C#-----Winform界面表格DataGridView的使用


       1.列宽度充满表格

    this.dgvStudentData.Columns["StudentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    this.dgvStudentData.Columns["StudentAge"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    this.dgvStudentData.Columns["StudentSex"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

       2.填充数据

    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 CBL.Player
    {
        public partial class frmStudentTable : Form
        {
            public frmStudentTable()
            {
                InitializeComponent();
            }
    
            private void frmStudentTable_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("StudentName", typeof(string));
                dt.Columns.Add("StudentAge", typeof(int));
                dt.Columns.Add("StudentSex", typeof(string));
    
                dt.Rows.Add(new object[] { "白起", 25, "" });
                dt.Rows.Add(new object[] { "李斯", 24, "" });
                dt.Rows.Add(new object[] { "王昭君", 23, "" });
    
                int i = 0;
                foreach (DataRow row in dt.Rows)
                {
                    this.dgvStudentData.Rows.Insert(i, row["StudentName"], row["StudentAge"], row["StudentSex"]);
                    i++;
                }
            }        
        }
    }

       3.添加行号

    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 CBL.Player
    {
        public partial class frmStudentTable : Form
        {
            public frmStudentTable()
            {
                InitializeComponent();
            }
    
            private void dgvStudentData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                SolidBrush b = new SolidBrush(this.dgvStudentData.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture),
                    this.dgvStudentData.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
            }
        }
    }

       4.单击单元格事件

    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 CBL.Player
    {
        public partial class frmStudentTable : Form
        {
            public frmStudentTable()
            {
                InitializeComponent();
            }
    
            private void dgvStudentData_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                //判断点击的单元格是哪一列
                bool flag = this.dgvStudentData.Columns[this.dgvStudentData.CurrentCell.ColumnIndex].Name == "StudentName";
                if (flag)
                {
                    string cellStudentName = dgvStudentData.Rows[e.RowIndex].Cells["StudentName"].Value.ToString();
                    string cellStudentAge = dgvStudentData.Rows[e.RowIndex].Cells["StudentAge"].Value.ToString();
                    string cellStudentSex = dgvStudentData.Rows[e.RowIndex].Cells["StudentSex"].Value.ToString();
                    MessageBox.Show(cellStudentName + "," + cellStudentAge + "," + cellStudentSex);
                }
            }
        }
    }

       5. 列标题及单元格居中

        this.dgvStudentData.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        this.dgvStudentData.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  • 相关阅读:
    操作系统的内存对齐机制学习笔记
    函数库调用和系统调用的区别
    操作系统与c语言
    技术开发人员适应其他部门提需求的一个经验
    受益技术类书籍
    软件项目发展历史<人月神话>这本书好
    好的代码风格积累
    演示内存碎片原理的好图
    不要的代码删除掉,而不是放到系统中干扰
    编译安装php-amq扩展
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11341796.html
Copyright © 2020-2023  润新知