zoukankan      html  css  js  c++  java
  • C#数据库绑定

    .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 }
  • 相关阅读:
    蝶恋花
    JVM解毒——JVM与Java体系结构
    超赞!IDEA 最新版本,支持免打扰和轻量模式!
    SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存
    神奇的 SQL 之 WHERE 条件的提取与应用
    终于放弃了单调的swagger-ui了,选择了这款神器—knife4j
    Git 高级用法,喜欢就拿去用
    既然有 HTTP 请求,为什么还要用 RPC 调用?
    SpringBoot和Spring到底有没有本质的不同?
    一条简单的更新语句,MySQL是如何加锁的?
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/4047586.html
Copyright © 2011-2022 走看看