winform datagridview 简单数据绑定
![](https://img2020.cnblogs.com/blog/2295966/202104/2295966-20210420202019494-304248260.png)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Oracle.ManagedDataAccess.Client; namespace windormTest { public partial class GridViewDemo : Form { public GridViewDemo() { InitializeComponent(); } // 學到的東西 //1.Oracle 綁定參數 是 :XXX ,sqlserver 是 @XXX //2.禁止datagridview生成表頭,前台無法設定,只能後台設定 //3.DataGridView 綁定的屬性名稱 是 displaypropertyname,而不是name private void button1_Click(object sender, EventArgs e) { dataGridView1.AutoGenerateColumns = false; try { string sql = @"select LOTNUM, layer, stknwip001, stknwip002, stknwip003, stknwip004, stknwip007, stknwip006 from pmmstknwip where lotnum=:lotnum"; string conn = ""; using (OracleConnection con = new OracleConnection(conn)) { con.Open(); using (OracleCommand cmd = new OracleCommand(sql,con)) { OracleParameter p = new OracleParameter("@lotnum", OracleDbType.Varchar2, 20); p.Value = this.textBox1.Text; //OracleParameter p = new OracleParameter(":lotnum", this.textBox1.Text); cmd.Parameters.Add(p); using (OracleDataAdapter oda = new OracleDataAdapter(cmd)) { DataTable dt = new DataTable(); oda.Fill(dt); this.dataGridView1.DataSource = dt; } } } //using (OracleConnection con = new OracleConnection(conn)) //{ // con.Open(); // using (OracleCommand cmd = con.CreateCommand()) // { // cmd.CommandText = sql; // OracleParameter p = new OracleParameter("@lotnum", this.textBox1.Text); // cmd.Parameters.Add(p); // OracleDataAdapter adapter = new OracleDataAdapter(cmd); // DataTable datatable = new DataTable(); // adapter.Fill(datatable); // } //} } catch (Exception e2) { MessageBox.Show(e2.Message); } } } }