DataGridView实例

本文介绍了一个使用 C# 和 SQL Server 实现的数据展示与编辑应用实例。该实例通过 DataGridView 控件展示数据库中的英雄角色信息,并实现了数据验证、错误处理、样式设置等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值