DataGridView详解

20 篇文章 0 订阅

DataGridView的主要常用属性都有哪些呢?

大多数都是UI界面样式属性设置(对于UI属性来说,容易理解,不再赘述),

主要介绍其他非UI属性:

1.CausesValidation 指示此组件是否引发验证事件;

2.如果在VS中添加了Columns集合,那么当从数据库中查询出数据时,会自动添加到这些列之后;

3.许多公共的事件,如鼠标移入移出等等;

下面对DataGridView的部分操作做实践:


using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace DataGridView数据控件
{
    public partial class Form1 : Form
    {
        SqlConnection conn;
        SqlDataAdapter sda;
        public Form1()
        {
            InitializeComponent();
        }
        //一:将数据库中表的数据加载到DataGridView控件中
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_myr;uid=sa;pwd=18404907723");
            sda = new SqlDataAdapter("select * from tb_user",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds,"user(自定义一个名字)");
            dataGridView1.DataSource = ds.Tables[0];
            //在用DataGridView控件显示数据的时候,可以将Columns[列的索引号]属性的Visible属性设置为false,以隐藏指定的列


            conn.Close();
            dataGridView2.DataSource = ds.Tables[0];
        }
        //二:获取DataGridView控件中的当前单元格
        //可以通过DataGridView控件的CurrentCell属性获取当前单元格信息
        //CurrentCell属性用于获取当前处于活动状态的单元格
        private void button1_Click(object sender, EventArgs e)
        {
            //使用CurrentCell.RowIndex和CurrentCell.ColumnIndex获取数据的行和列
            string msg = String.Format("第{0}行,第{1}列", dataGridView1.CurrentCell.RowIndex, dataGridView1.CurrentCell.ColumnIndex);
            label1.Text = "选择的单元格为:" + msg;
            //可以通过DataGridView控件的SelectedCells属性集获取该控件中被选中的单元格信息
        }
        //三:DataGridView中修改数据
        //用到DataTable的ImportRow方法和DataAdapter对象的Update方法
        //实现过程是:通过DataTable的ImportRow方法将更改后的数据复制到一个DataTable中,然后通过DataAdapter的Update方法,将DataTable中的数据更新到数据库中
        //public void ImportRow(DataRow row)  将DataRow复制到DataTable中,保留任何属性设置以及初始值和当前值   row要导入的DataRow
        private void button2_Click(object sender, EventArgs e)
        {
            //禁止显示行标题
            dataGridView2.RowHeadersVisible = false;
            //使用for循环设置控件的列宽
            for(int i=0;i<dataGridView1.ColumnCount;i++)
            {
                dataGridView2.Columns[i].Width = 500;
            }
            //禁用按钮
            button2.Enabled = false;
            //将控件设置为只读
            dataGridView2.Columns[0].ReadOnly = true;
        }
        //建立一个DataTable类型
        private DataTable dbconn(string strSql)
        {
            //conn.Open();
            this.sda = new SqlDataAdapter(strSql, conn);
            DataTable dtSelect = new DataTable();
            int rnt = this.sda.Fill(dtSelect);
            conn.Close();
            return dtSelect;
        }
        //更新数据
        private void button3_Click(object sender, EventArgs e)
        {
            if (dbUpdate()) 
            {
                MessageBox.Show("修改成功");
            }
        }
        //更新数据
        private Boolean dbUpdate()
        {
            string strSql = "select * from tb_user";
            DataTable dtUpdate = new DataTable();
            dtUpdate = this.dbconn(strSql);
            dtUpdate.Clear();
            DataTable dtShow = new DataTable();
            dtShow =(DataTable)this.dataGridView2.DataSource;
            for(int i=0;i<dtShow.Rows.Count;i++)
            {
                dtUpdate.ImportRow(dtShow.Rows[i]);
            }
            try
            {
                this.conn.Open();
                SqlCommandBuilder CommandBuilder;
                CommandBuilder = new SqlCommandBuilder(this.sda);
                this.sda.Update(dtUpdate);
                this.conn.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return false;
            }
            dtUpdate.AcceptChanges();//提交更改
            return true;
        }


        //三:当选中DataGridView控件中的行时显示不同的颜色
        //利用DataGridView控件的: 
        //          1.SelectionMode,用于设置如何选择DataGridView单元格,返回值是DataGridViewSelectionMode的枚举值之一
        //                    默认是RowHeaderSelect,单击标头行选中此行,单击某个单元格可以选定此单元格
        //                    ellSelect,可以选定一个或者多个单元格
        //                    ColumnHeaderSelect,单击列的标头单元选中此列,单击某个单元格选中独特的单元格
        //                    FullColumnSelect,单击列的标头或者该列所属的单元格选定此列
        //                    FullRowSelect,单击行的标头或者该行所包含的单元格选定此行  
        //          2. ReadOnly,用于是否可以编辑DataGridView控件的单元格,返回值shibool
        //          3. SelectionBackColor,单元格的背景色,默认为Empty,该属性包含在DataGridViewCellStyle中,所以调用此属性之前要调用DataGridViewCellStyle属性
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;


            dataGridView1.AllowUserToAddRows = true;
            dataGridView1.AllowUserToDeleteRows = true;
            //dataGridView1.ReadOnly = false;
        }


        //四:禁止在DataGridView控件中添加和删除行
        //使用DataGridView的公共属性AllowUserToAddRows,AllowUserToDeleteRows,ReadOnly
        //该操作在button4_Click中实现
        
        //五:使用Columns和Rows属性添加数据
        //使用DataGridView控件的Columns和Rows属性值
        //Columns属性用于获取一个包含控件中所有列的集合
        //public DataGridViewColumnCollection Columns{get;}   DataGridViewColumnCollection,包含DataGridView控件中的所有列
        //public DataGridViewRowCollection Rows{get;}         DataGridViewRowCollection,包含DataGridView控件中的所有行
        //如果想在DataGridView控件的单元格中添加下拉列表,可以通过DataGridViewComboBoxColumn类来实现
        private void button5_Click(object sender, EventArgs e)
        {
            //指定DataGridView控件显示的列数
            dataGridView3.ColumnCount = 7;
            dataGridView3.ColumnHeadersVisible = true;//显示列标题
            //设置DataGridView控件标题列的样式
            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
            //设置标题列的背景颜色
            columnHeaderStyle.BackColor = Color.Beige;
            //设置列标题的字体大小,样式
            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
            dataGridView3.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
            //设置DataGridView控件的标题列名
            dataGridView3.Columns[0].Name = "编号";
            dataGridView3.Columns[1].Name = "姓名";
            dataGridView3.Columns[2].Name = "性别";
            dataGridView3.Columns[3].Name = "年龄";
            dataGridView3.Columns[4].Name = "地址";
            dataGridView3.Columns[5].Name = "薪资";
            dataGridView3.Columns[6].Name = "部门";
            //建立2两行数据
            string[] row1 = new string[] { "001", "哈萨给", "1","52","山西","4500","3" };
            object[] rows = new object[] { row1};
            //使用for语句循环添加
            foreach(string[] rowArray in rows)
            {
                dataGridView3.Rows.Add(rowArray);
            }
        }
    }
}

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值