using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string conStr = "server=.;database=Demo;uid=sa;pwd=yujie1127";
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from hero", con);
da.Fill(ds);
con.Close();
//让列头显示中文
this.dataGridView1.Columns.Add("","编号");
this.dataGridView1.Columns[0].DataPropertyName = "id";
this.dataGridView1.Columns.Add("", "名字");
this.dataGridView1.Columns[1].DataPropertyName = "name";
this.dataGridView1.Columns.Add("", "性别");
this.dataGridView1.Columns[2].DataPropertyName = "Sex";
this.dataGridView1.Columns.Add("", "年龄");
this.dataGridView1.Columns[3].DataPropertyName = "Age";
this.dataGridView1.Columns.Add("", "兵器");
this.dataGridView1.Columns[4].DataPropertyName = "binqi";
this.dataGridView1.Columns.Add("", "特长");
this.dataGridView1.Columns[5].DataPropertyName = "techang";
//设置DataGridView的背景色
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
this.dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.LightBlue;
this.dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.LightBlue;
this.dataGridView1.Columns[4].DefaultCellStyle.BackColor = Color.LightBlue;
//设置DataGridView控件基于文本的单元格启用换行
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//设置DataGridView控件单元格的文本对齐方式
this.dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//窗体关闭时退出应用程序
Application.Exit();
}
//验证数据输入
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 0)
{
if(String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText = "单元格第一列值不能为空!";
e.Cancel = true;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
//输入数据发生的错误
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (e.Exception != null)
{
MessageBox.Show(e.Exception.ToString(), "输入错误信息提示。");
}
}
//设置网格样式
private void btnGrid_Click(object sender, EventArgs e)
{
this.dataGridView1.GridColor = Color.BlueViolet;
}
//设置边框样式
private void btnBorder_Click(object sender, EventArgs e)
{
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
}
//字体样式
private void btnFont_Click(object sender, EventArgs e)
{
this.dataGridView1.DefaultCellStyle.Font = new Font("宋体", 14);
}
//获取当前单元格
private void btnCurrentCell_Click(object sender, EventArgs e)
{
string mst = string.Format("Row{0},Column{1}", dataGridView1.CurrentCell.RowIndex+1,
dataGridView1.CurrentCell.ColumnIndex+1);
MessageBox.Show(mst);
}
//选中单元格时整个背景变色
private void btnChangeColor_Click(object sender, EventArgs e)
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.ReadOnly = true;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.White;
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Maroon;
}
}
}