• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

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;

 

posted @ 2019-08-13 09:14  风拂晚柳  阅读(4274)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3