1.简单使用
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApp17 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
People p0 = new People(0, "liuyan", 20 , "打球");
People p1 = new People(1, "chenghang", 21 , "跑步");
People p2 = new People(2, "study", 22 ,"健身");
List<People> peoples = new List<People>();
peoples.Add(p0);
peoples.Add(p1);
peoples.Add(p2);
//this.dataGridView1.DataSource = peoples; 直接传数据对象,列标题为变量名
//列标题 (无法直接设置,需要先在界面中添加3个默认列)
this.dataGridView1.Columns[0].HeaderText = "ID";
this.dataGridView1.Columns[1].HeaderText = "年龄";
this.dataGridView1.Columns[2].HeaderText = "爱好";
//添加行数据
this.dataGridView1.Rows.Add(p0.ID , p0.AGE , p0.HOBBY);
this.dataGridView1.Rows.Add(p1.ID, p1.AGE, p1.HOBBY);
this.dataGridView1.Rows.Add(p2.ID, p2.AGE, p2.HOBBY);
//要先有行数据,才能设置行标题 一般行标题为序号,与数据不联系
this.dataGridView1.Rows[0].HeaderCell.Value = p0.NAME;
this.dataGridView1.Rows[1].HeaderCell.Value = p1.NAME;
this.dataGridView1.Rows[2].HeaderCell.Value = p2.NAME;
//隐藏行标题
//this.dataGridView1.RowHeadersVisible = false;
//因为行标题的内容显示不全,需要设置宽度和或模式
this.dataGridView1.RowHeadersWidth = 100;
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
//因为最后一行时多余的,与数据无关,所以需要去掉。无法直接删除或隐藏最后一行
this.dataGridView1.AllowUserToAddRows = false;
}
private void button1_Click(object sender, EventArgs e) {
//当前选中单元格的内容
//MessageBox.Show(this.dataGridView1.CurrentCell.Value.ToString());
}
}
public class People {
private int id;
private String name;
private int age;
private String hobby;
public int ID {
get { return id; }
set { this.id = value; }
}
public String NAME {
get { return name; }
set { this.name = value; }
}
public int AGE {
get { return age; }
set { this.age = value; }
}
public String HOBBY {
get { return hobby; }
set { this.hobby = hobby; }
}
public People(int id , String name , int age,String hobby) {
this.id = id;
this.name = name;
this.age = age;
this.hobby = hobby;
}
}
}
2.查询数据库数据,直接显示到DataGridView
String sqlstring = "Data Source=.;Initial Catalog=liuyan;Integrated Security=True";
SqlConnection con = new SqlConnection(sqlstring);
String sqls = @"SELECT * FROM [表名]";
SqlCommand cmd = new SqlCommand(sqls , con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet,"别名");
//这个不需要事先在视图中添加默认列
this.dataGridView1.DataSource = dataSet.Tables["别名"];
this.dataGridView1.AllowUserToAddRows = false;
con.Close();
3.添加行序号,并且添加右键菜单。删除某一行时,先删除数据库的内容,再删除表格的内容,且序号会自动重绘。
要用到右键菜单,需要在窗体添加一个鼠标菜单控件 ContextMenuStrip
该控件不会直接显示在窗体上,而是有鼠标点击事件时,才会根据自己的需求显示在任意位置。
在鼠标菜单中,我只添加了一个菜单项“删除行”
代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
initDataGridView();
}
//DatGrideView填充数据库数据
private void initDataGridView() {
String sqlstring = "Data Source=.;Initial Catalog=liuyan;Integrated Security=True";
SqlConnection con = new SqlConnection(sqlstring);
String sqls = @"SELECT * FROM [user]";
SqlCommand cmd = new SqlCommand(sqls, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "别名");
this.dataGridView1.DataSource = dataSet.Tables["别名"];
this.dataGridView1.AllowUserToAddRows = false;
con.Close();
this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
//在标题列中绘制行号
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) {
//行号,不包括标题行,从0开始,包括标题列,每个单元格的左上角的坐标
Debug.WriteLine(e.RowIndex + " "+ e.RowBounds.X + " " + e.RowBounds.Y);
//方法①
Font font = this.dataGridView1.DefaultCellStyle.Font;
SolidBrush brush = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
e.Graphics.DrawString(e.RowIndex + 1+"", font, brush, new PointF(e.RowBounds.X+20,e.RowBounds.Y+4));
//方法②
//Rectangle rectangle = new Rectangle(e.RowBounds.X, e.RowBounds.Y, this.dataGridView1.RowHeadersWidth, e.RowBounds.Height);
//TextRenderer.DrawText(e.Graphics, e.RowIndex+1+"", font, rectangle, this.dataGridView1.ForeColor);
}
//单元格鼠标右键点击,弹出鼠标菜单ContextMenuStrip
int index;
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
if (e.Button == MouseButtons.Right && index >= 0) {
//下面的两行代码是为了表格显示合理
this.dataGridView1.Rows[e.RowIndex].Selected = true;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
index = e.RowIndex;
//显示菜单
this.contextMenuStrip1.Show(Cursor.Position);
}
}
//点击菜单栏中的某项
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
if (e.ClickedItem.Text.Equals("删除行")) {
String sqlstring = "Data Source=.;Initial Catalog=liuyan;Integrated Security=True";
SqlConnection con = new SqlConnection(sqlstring);
con.Open();
String sqls = @"DELETE FROM [user] WHERE MYNAME = '"+this.dataGridView1.CurrentCell.Value+"'";
SqlCommand cmd = new SqlCommand(sqls, con);
//先删除数据库
if (cmd.ExecuteNonQuery() != -1){
//再删除表
this.dataGridView1.Rows.RemoveAt(index);
}
con.Close();
}
}
}
}