using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using ConsoleExtClass; using DataGridViewDemo01.Helpers; using MessageBoxer; namespace DataGridViewDemo01 { /// <summary> /// WinForm里DataGridView默认样式设置(添加序号列) /// LDH @ 2021-10-22 /// </summary> public partial class FrmMain : Form { #region Global Variables /// <summary> /// 创建学生列表 /// </summary> private readonly List<Student> _list = new List<Student>(); #endregion #region Constructor public FrmMain() { InitializeComponent(); } #endregion #region My Methods /// <summary> /// 绑定学生列表 /// </summary> private void BindStudents() { dgvStudents.DataSource = null; // 清空绑定 dgvStudents.DataSource = _list; // 指定数据源 dgvStudents.AllowUserToResizeRows = false; // 禁止用户改变DataGridView所有行的行高 dgvStudents.AutoGenerateColumns = false; // 禁止自动生成列 #region 【LDH珍藏的样式V2021.10】 // 设置样式(最爱) DataGridViewHelperV2.SetStyleForDataGridView( dgvStudents, Color.FromArgb(192, 139, 165), Color.White, Color.FromArgb(213, 186, 199), Color.FromArgb(249, 250, 249), Color.FromArgb(238, 229, 234), false, Color.GhostWhite, true); #endregion } /// <summary> /// 设置控件 /// </summary> private void SetControls() { MaximizeBox = false; // 禁用最大化按钮 AutoSizeMode = AutoSizeMode.GrowAndShrink; // 不让窗体右下角拖拽 dgvStudents.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // DataGridView 整行选取 dgvStudents.AllowUserToAddRows = false; // 不向用户显示添加行的选项 dgvStudents.AllowUserToResizeRows = false; // 禁止用户改变DataGridView所有行的行高 dgvStudents.AutoGenerateColumns = false; // 禁止自动生成列 // 添加序号(最左侧列) //dgvStudents.RowsAdded += DgvStudents_RowsAdded; //dgvStudents.RowsRemoved += DgvStudents_RowsRemoved; } #endregion #region Form Events /// <summary> /// 添加学生 /// </summary> private void btnAdd_Click(object sender, EventArgs e) { var stu = new Student { StuNo = txtStuNo.Text, StuName = txtStuName.Text, StuSex = cboStuSex.Text, StuAge = Convert.ToInt32(nupStuAge.Value) }; _list.Add(stu); BindStudents(); ConsoleExt.InfoLine("添加成功!"); MessageTip.ShowOk("添加成功!"); } /// <summary> /// 删除行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, EventArgs e) { // 共选中的行数 var selectedRowCount = dgvStudents.SelectedRows.Count; if (selectedRowCount > 0) { var dr = ShowMessage.GetUserYesOrNo("您是否要删除选择的记录呢?", "温馨提示"); if (dr == DialogResult.Yes) { // 遍历所有选中的行 for (var i = 0; i < selectedRowCount; i++) { var index = dgvStudents.SelectedRows[i].Index; _list.RemoveAt(index); } // 重新绑定 BindStudents(); MessageTip.ShowOk("删除成功!"); ConsoleExt.InfoLine("删除成功!"); } else { MessageTip.ShowOk("您取消了删除操作!"); ConsoleExt.WarnLine("您取消了删除操作!"); } } else { tipDelete.Show("请先选择您要删除的行!", btnDelete, 0, -btnDelete.Height, 5000); ConsoleExt.ErrorLine("请先选择您要删除的行!"); } } /// <summary> /// 窗体加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_Load(object sender, EventArgs e) { SetControls(); } /// <summary> /// 窗体关闭事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { var dr = ShowMessage.GetUserYesOrNo("您确定现在退出系统吗?", "温馨提示"); if (dr == DialogResult.Yes) Environment.Exit(0); else e.Cancel = true; } #region 【添加序号(最左侧列)】 private void DgvStudents_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) { var dgv = (DataGridView)sender; if (dgv.Rows.Count != 0) { for (var i = 0; i < e.RowCount; i++) { dgv.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; dgv.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString(); } for (var i = e.RowIndex + e.RowCount; i < dgv.Rows.Count; i++) { dgv.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; dgv.Rows[i].HeaderCell.Value = (i + 1).ToString(); } } } private void DgvStudents_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { var dgv = (DataGridView)sender; for (var i = 0; i < e.RowCount; i++) { dgv.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; dgv.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString(); } for (var i = e.RowIndex + e.RowCount; i < dgv.Rows.Count; i++) { dgv.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; dgv.Rows[i].HeaderCell.Value = (i + 1).ToString(); } } #endregion #endregion } }
namespace DataGridViewDemo01 { public class Student { public string StuNo { get; set; } public string StuName { get; set; } public string StuSex { get; set; } public int StuAge { get; set; } } }