Use C # ----- Winform DataGridView form of interface

   1. Column full width form

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

   2. Fill the data

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. Add line numbers

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. Click cell event

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);
            }
        }
    }
}

   The column header and the cell center

    this.dgvStudentData.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    this.dgvStudentData.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

 

Guess you like

Origin www.cnblogs.com/fengfuwanliu/p/11341796.html