zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    sql去重复
    验证 decimal 和 数字
    OleDbConnection读取Excel
    排班知识点
    sql基础
    SQL Server 获取月份的具体天数
    2016 Excel 展示 Sqlserver数据并制作图表
    SQL Server跨域查询
    SqlSugar中CASE WHEN的用法
    Microsoft.AspNetCore.Mvc.Versioning学习笔记
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11341796.html
Copyright © 2011-2022 走看看