.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 }