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;