using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common; namespace DataGridViewDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} string conStr = "server=localhost;database=db_EMS;integrated security=true";
SqlConnection conn;
SqlDataAdapter da;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
//禁止添加行
dataGridView1.AllowUserToAddRows = false;
//禁止刪除行
dataGridView1.AllowUserToDeleteRows = false;
conn = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from tb_pdic", conn);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//禁止對列進行排序(這個要在賦值datasource後)
for (int i = 0; i < dataGridView1.Columns.Count; i++)
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
//選中時選中整行
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//不同的行賦值不同的顏色
foreach(DataGridViewRow dgv in dataGridView1.Rows)
{
if(dgv.Index%2==0)
{
dataGridView1.Rows[dgv.Index].DefaultCellStyle.BackColor = Color.LightSalmon;
}
else
{
dataGridView1.Rows[dgv.Index].DefaultCellStyle.BackColor = Color.LightPink;
}
}
dataGridView1.ReadOnly = true;
//設置選中行的顏色
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int id =(int) dataGridView1.Rows[e.RowIndex].Cells[0].Value;
conn = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from tb_pdic where id=" + id, conn);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
textBox1.Text = ds.Tables[0].Rows[0]["name"].ToString();
textBox2.Text = ds.Tables[0].Rows[0]["money"].ToString();
}
}
}
}

DataGridView控件使用Demo的更多相关文章

  1. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  2. 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)

    实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...

  3. DataGridView控件

    DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...

  4. DataGridView控件-[引用]

    DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...

  5. DataGridView控件使用大全说明-各种常用操作与高级操作

    DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...

  6. 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码

    Windows窗体DataGridView控件的性能调优.net 4.5   在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...

  7. 使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体

    使用 DataGridView 控件的一种最常见方案是“主/详细信息”窗体,这样的窗体可显示两个数据库表之间的父/子关系.如果选择主表中的行,将导致以相应的子数据来更新详细信息表. 主/详细信息窗体很 ...

  8. DataGridView控件使用大全

    转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...

  9. Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010

    摘  要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...

随机推荐

  1. Docker安装RabbitMQ与Kafka

    RabbitMq安装(dokcer) 下载镜像 docker pull rabbitmq 创建并启动容器 docker run -d --name rabbitmq -p 5672:5672 -p 1 ...

  2. mysqldump备份数据

    create database test; use test; create table test(id int,name char(8)); insert into test values(1,'p ...

  3. pandas的学习1-基本介绍

    ''' Numpy 和 Pandas 有什么不同 如果用 python 的列表和字典来作比较, 那么可以说 Numpy 是列表形式的,没有数值标签,而 Pandas 就是字典形式.Pandas是基于N ...

  4. Yii 文件上传类的使用

    https://segmentfault.com/a/1190000009674814 以上链接的文章已经写的很好了,一目了然,这里不再多做说明. 补充:当执行 UploadedFile->sa ...

  5. ADO.NET 帮助类 参数传递 存储过程 分页

    SQLHelper public class SqlHelper { private readonly string _constr = ConfigurationManager.Connection ...

  6. 分布式事务MSDTC使用时,需要的配置

    服务器最终配置 DTC服务 组件 防火墙 这里,跟下面的解决方案有点差异,在添加2个规则之后,原本就有分布式相关的规则,也给开启了. 网上的解决办法 在服务里打开 Distributed Transa ...

  7. 基于LNMP架构搭建wordpress博客之安装架构说明

    架构情况 架构情况:基于LNMP架构搭建wordpress系统 软件包版本说明: 系统要求 :  CentOS-6.9-x86_64-bin-DVD1.iso PHP版本  :  php-7.2.29 ...

  8. 手把手教你搭饥荒专用服务器(五)—MOD自动下载安装(Windows+Linux)

    想了解更详细内容,请点击原文地址:https://wuter.cn/1985.html/ 饥荒专用服务器的mod设置总共有两种方法. 方法一 在本地游戏中更新mod,然后把mod上传到服务器,但是这种 ...

  9. Git 仓库拆分

    方案对比 subtree 使用命令 git subtree split -P dirPath -b branchName 将目标文件夹的代码都保存到指定分支.试了下,该方案虽然保留了 commit,但 ...

  10. python3使用configparser读取配置文件

    python2中的ConfigParser在python3中改成了configparser 1.配置文件格式是 [域名] k=v 2.代码示例:需要生成conf.ini配置文件如下:[config]v ...