添加特定列 c# datagridview
Add specific column c# datagridview
我已经创建了 table 供应商 1`创建 table 供应商 1(
> sup_id NUMBER(20) PRIMARY KEY, sup_name VARCHAR2(40), sup_address
> varchar2(50), sup_phone NUMBER(15))
;
并插入值
`
> INSERT INTO suppliers1 (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (100,'PPS','Bds',99545414); INSERT INTO suppliers1
> (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (200,'Abcd','Dhaka',0295469); INSERT INTO suppliers1
> (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (300,'Xyz','Ctg',0896547556);
我使用以下命令加载数据
> OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM
> suppliers1", con);
> DataTable dt = new DataTable();
> adp.Fill(dt);
> dataGridView1.DataSource = dt;
但我想以编程方式添加特定列,例如 sup_id 或 sup_name 等,我已经尝试了代码但失败了怎么可能?
String testValue2 = (String)dataGridView1["sup_id", 4]value;
datagridView1.Columns.Add(...)
有几个选项,制作字符串列最简单的就是提供名称。您还可以提供一个类型,或者您可以创建一个 DataGridViewColumn 实例并将其传入,以获得最大的灵活性
这是文档:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columns(v=vs.110).aspx
试试简单的代码
OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM suppliers1", con);
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 2;// you can add more
dataGridView1.Columns[0].HeaderText = "Sup Id";
dataGridView1.Columns[0].DataPropertyName = "sup_id";
dataGridView1.Columns[1].HeaderText = "Sup Name";
dataGridView1.Columns[1].DataPropertyName = "sup_name";
dataGridView1.DataSource = dt;
我已经创建了 table 供应商 1`创建 table 供应商 1(
> sup_id NUMBER(20) PRIMARY KEY, sup_name VARCHAR2(40), sup_address
> varchar2(50), sup_phone NUMBER(15))
;
并插入值 `
> INSERT INTO suppliers1 (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (100,'PPS','Bds',99545414); INSERT INTO suppliers1
> (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (200,'Abcd','Dhaka',0295469); INSERT INTO suppliers1
> (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) VALUES
> (300,'Xyz','Ctg',0896547556);
我使用以下命令加载数据
> OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM
> suppliers1", con);
> DataTable dt = new DataTable();
> adp.Fill(dt);
> dataGridView1.DataSource = dt;
但我想以编程方式添加特定列,例如 sup_id 或 sup_name 等,我已经尝试了代码但失败了怎么可能?
String testValue2 = (String)dataGridView1["sup_id", 4]value;
datagridView1.Columns.Add(...)
有几个选项,制作字符串列最简单的就是提供名称。您还可以提供一个类型,或者您可以创建一个 DataGridViewColumn 实例并将其传入,以获得最大的灵活性
这是文档:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columns(v=vs.110).aspx
试试简单的代码
OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM suppliers1", con); DataTable dt = new DataTable(); adp.Fill(dt); dataGridView1.AutoGenerateColumns = false; dataGridView1.ColumnCount = 2;// you can add more dataGridView1.Columns[0].HeaderText = "Sup Id"; dataGridView1.Columns[0].DataPropertyName = "sup_id"; dataGridView1.Columns[1].HeaderText = "Sup Name"; dataGridView1.Columns[1].DataPropertyName = "sup_name"; dataGridView1.DataSource = dt;