DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

一、DataGridView属性设置

1、我们单击选中行的时候,默认是选择一个单元格,不能选择一整行,我们只需设置DataGridView的属性SelectionMode为FullRowSelect 。用代码表示:this.dataGridView1.SelectionMode =DataGridViewSelectionMode.FullRowSelect;

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

 

2、选择多行,可设置DataGridView的属性MultiSelect为false 。 用代码表示:this.dataGridView1.MultiSelect = false;这样就使DataGridView不能够选择多行,只能选择一行了 

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

 

3、是否自动创列:dataGridView1.AutoGenerateColumns = false;

 

4、DataGridView这个控件会默认的在第一行第一列选中。这个问题我在网上看到了很多回答,也有很多人问。可能是我用的方法不对,效果不是很好后来找到了这种答案:

  dataGridView1.ClearSelection();一行就可以。

 

5、设置标题样式&字体:首先把这个“EnableHeadersVisualStyles”属性设置为false。

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

在然后设置标题的样式,设置这个属性“ColumnHeadersDefaultCellStyle”:

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

 

6、设置行的样式,设置DefaultCellStyle属性:

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

 

二、DataGridView事件

1、单击项或双击行时,获取行的数据(CellClick)

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...
 1 private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
 2         {
 3 
 4             if (e.RowIndex != -1)//判断是否点在行上
 5             {
 6                 txt_No.Text = this.DGV["Col_No", e.RowIndex].Value.ToString();
 7                 txt_Name.Text = this.DGV["Col_Name", e.RowIndex].Value.ToString();
 8                 txt_Type.Text = this.DGV["Col_Type", e.RowIndex].Value.ToString();
 9                 txt_Time.Text = this.DGV["Col_Time", e.RowIndex].Value.ToString();
10             }
11         }
View Code

“Col_No”。“Col_Name”,“Col_Type”,“Col_Time”是列名。

 

2、在单元格的内容需要设置格式以便于显示时发生。(CellFormatting)

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...
 1 /// <summary>
 2 /// 如果列名Col_IsEnabled列的值为1则显示启用,否则显示禁用
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 private void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
 7 {
 8     if (DGV.Columns[e.ColumnIndex].Name == "Col_IsEnabled")
 9     {
10         if ((e.Value).ToString().Trim() == "1")
11         {
12             e.Value = "启用";
13         }
14         else
15         {
16             e.Value = "禁用";
17         }
18     }
19 }
改变列值

 

代码示例:

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...
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;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "Col_IsEnable")
                {
                    if ((e.Value).ToString().Trim() == "1")
                    {
                        e.Value = "启用";
                        e.CellStyle.ForeColor = Color.Red;
                        dataGridView1.Columns[e.ColumnIndex].Width = 50; //设置列宽

                    }
                    else if ((e.Value).ToString().Trim() == "2")
                    {
                        e.Value = "禁用";
                        e.CellStyle.ForeColor = Color.Green;//设置字体前景
                        e.CellStyle.BackColor = Color.Purple;//设置背景色
                        dataGridView1.Rows[e.RowIndex].Height = 50;//设置行高
                    }
                }
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                textBox1.Text = this.dataGridView1["Col_Id", e.RowIndex].Value.ToString();
                textBox2.Text = this.dataGridView1["Col_Name", e.RowIndex].Value.ToString();
                textBox3.Text = this.dataGridView1["Col_Age", e.RowIndex].Value.ToString();
                textBox4.Text = this.dataGridView1["Col_Sex", e.RowIndex].Value.ToString();
                comboBox1.Text = this.dataGridView1["Col_IsEnable", e.RowIndex].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = new DataGridViewRow();
            dr.CreateCells(dataGridView1);
            dr.Cells[0].Value = textBox1.Text.ToString().Trim();
            dr.Cells[1].Value = textBox2.Text.ToString().Trim();
            dr.Cells[2].Value = textBox3.Text.ToString().Trim();
            dr.Cells[3].Value = textBox4.Text.ToString().Trim();
            dr.Cells[4].Value = comboBox1.Text.ToString();
            // dataGridView1.Rows.Insert(0, dr);     //插入的数据作为第一行显示
            dataGridView1.Rows.Add(dr);                    //插入的数据作为最后一行显示
        }

    }
}
View Code

DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):...

 

3、某一列不可编辑:

 DGV.Columns["列名"].ReadOnly = true;

或:

 DGV.Columns[i].ReadOnly = true;