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

最新文章

  1. 使用scvmm 2012的动态优化管理群集资源
  2. javascript判断图片是否加载完成方法整理
  3. Windows Azure初体验
  4. 在Grunt task中集成Protractor
  5. MySQL中快速复制数据表方法汇总
  6. compile error
  7. Windows 设置扩展投影鼠标移出方向
  8. c3p0数据库连接池
  9. DICOM:DICOM3.0网络通信协议
  10. Find命令搜索过去一段时间内修改过的文件
  11. Redirect 原理
  12. 更改JENKINS主目录
  13. 由闭包引起的对javascript代码可维护性的思考
  14. Libreoffice汉化
  15. 【电视桌面CSWUI】电视桌面(launcher)截图欣赏
  16. 使用google搜索时的10个小技巧!
  17. markdown test2
  18. C# Socket的粘包处理
  19. 本地项目初始化git推送到服务器
  20. linux 拷贝软连接文件

热门文章

  1. MySQL全面瓦解14:事务
  2. mysql难题收录
  3. 解析SwiftUI布局细节(一)
  4. 登录linux时 shell执行顺序
  5. js上 十九、综合案例
  6. js上 十八、字符串
  7. [实用指南]如何使您的旧代码库(遗留代码)符合MISRA C 2012编码规范?
  8. Spring Cloud正式移除Hystrix、Zuul等Netflix OSS组件
  9. python三大流程
  10. web基础知识,