• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#数据库绑定

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

.Net对数据库的绑定

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.OracleClient;
 11 using System.Data.OleDb;
 12 
 13 namespace WindowsFormsApplication2
 14 {
 15     public partial class Form1 : Form
 16     {
 17         public Form1()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         private void BuildColor()
 23         {
 24             for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
 25             {
 26                 if (this.dataGridView1[0, i] != null && this.dataGridView1[0, i].Value != null)
 27                 {
 28                     // 根据条件设置不同的颜色
 29                     if (this.dataGridView1[6, i].Value.ToString() == "3")
 30                         this.dataGridView1[6, i].Style.BackColor = Color.Blue;
 31                     else if (this.dataGridView1[6, i].Value.ToString() == "10")
 32                         this.dataGridView1[6, i].Style.BackColor = Color.Yellow;
 33                 }
 34             }
 35         }
 36 
 37         private void btnGetData_Click(object sender, EventArgs e)
 38         {
 39             using (OracleConnection conn =
 40             new OracleConnection("data source=10.21.144.152/JXWSQZJ;User Id=qzj_bak;Password=qzj_bak;"))
 41             {
 42                 OracleCommand cmd = conn.CreateCommand();
 43                 cmd.CommandText = "select *  from TB_YL_MZ_MEDICAL_RECORD where rownum<100";
 44                 conn.Open();
 45 
 46                 OracleDataReader odr = cmd.ExecuteReader();
 47                 OracleDataAdapter sda = new OracleDataAdapter(cmd);
 48                 DataSet ds = new DataSet();
 49                 sda.Fill(ds, "TB_YL_MZ_MEDICAL_RECORD");
 50                 dataGridView1.DataSource = ds;
 51                 dataGridView1.DataMember = "TB_YL_MZ_MEDICAL_RECORD";   //上面两句等价于 dataGridView1.DataSource = ds.Tables["TB_YL_MZ_MEDICAL_RECORD"]
 52                 dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
 53                 dataGridView1.Columns[0].HeaderText = "测试";
 54                 foreach (DataGridViewColumn col in dataGridView1.Columns)
 55                 {
 56                     if (col.Name == "KH")
 57                     { col.HeaderText = "aaaa"; }
 58                 }
 59                 this.BuildColor();
 60             }
 61         }
 62 
 63         private void Form1_Load(object sender, EventArgs e)
 64         {
 65             // TODO: 这行代码将数据加载到表“yygl_jxDataSet.CLGL_Import”中。您可以根据需要移动或删除它。
 66             //dataGridView1.Dock = DockStyle.Fill;
 67             //dataGridView1.DataSource = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();
 68         }
 69 
 70         private void cLGLImportBindingSource_CurrentChanged(object sender, EventArgs e)
 71         {
 72 
 73         }
 74 
 75         private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
 76         {
 77             System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
 78         e.RowBounds.Location.Y,
 79         dataGridView1.RowHeadersWidth - 4,
 80         e.RowBounds.Height);
 81 
 82             TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
 83                 dataGridView1.RowHeadersDefaultCellStyle.Font,
 84                 rectangle,
 85                 dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
 86                 TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
 87         }
 88 
 89         private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
 90         {
 91             for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
 92             {
 93                 if (i % 2 == 0)
 94                 {
 95                     this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
 96                     //this.dataGridView1.Rows[i].DefaultCellStyle.Font = Font.;
 97                 }
 98                 else
 99                 {
100                     this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
101                     //this.dataGridView1.Rows[i].DefaultCellStyle.Font = this.splitContainer1.Font;
102                 }
103             }
104         }
105     }
106 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap