添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient; //引入oracle的连接对象命名空间

namespace learncomboxanddatagridview
{
public partial class Form1 : Form
{
//public  DataSet ds1 = new DataSet();
public OracleDataAdapter da1 = new OracleDataAdapter();
public DataTable dt1=new DataTable();
public int index1;
public Form1()
{
InitializeComponent();
}

//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
//属性是否允许用户在列表datagridview中添加或删除数据
//dataGridView1.AllowUserToAddRows = false;
//dataGridView1.AllowUserToDeleteRows = false;

//学习如何显示不显示列表的列标题
dataGridView1.ColumnHeadersVisible = true;

//学习设置列表的列标题的样式格式设置(字体及大小及其它,粗细体等)
DataGridViewCellStyle. columnheaderstyle. = new DataGridViewCellStyle();
columnheaderstyle.BackColor = Color.Brown;
//font.bold返回类型为布尔bool
columnheaderstyle.Font = new Font("Verdana",10,FontStyle.Bold);

//此步很重要,就让列表的列表头以上述设置的样式进行显示
dataGridView1.ColumnHeadersDefaultCellStyle. = columnheaderstyle;



//oraclconnection连接数据库字符串为data source,user id,
//1,连接数据库
OracleConnection con1 = new OracleConnection("Data Source=orcl;User id=scott; Password=system;");//oracleconnection类隶属于命名空间system.data.oracleclient
//2,连接数据库成功,生成执行sql脚本
OracleCommand oc1 = new OracleCommand("select deptno,dname,loc from dept", con1);
//OracleDataReader dr = oc1.ExecuteReader();
//3,生成存放sql运行结果的容器
DataSet ds1 = new DataSet();


//5,通过适配器的属性把上述命令sql脚本与适配器关联,即让适配器执行上述sql
da1.SelectCommand = oc1;
//6,通过适配器的fill方法向空器填充数据
da1.Fill(dt1);
//通过展示数据列表的datasource属性与上述已填充数据的空器进行关联.记得空器可能包含多个表,要用dataset.tables[0],仅提取一个表
this.dataGridView1.DataSource = dt1;
}
//datagridview的事件cellclick为单击列表单元格任何部分会触发此事件
//实现单击列表某一行把对应数据显示在列表下方的对应文本框中

//selectionchanged事件为选中列表不同行触发此事件
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
index1 = dataGridView1.CurrentRow.Index;
//cells表示每行每个列,一定要在后面添加value.tostring,不然提示转换错误
this.textBox1.Text =(string)dataGridView1.Rows[index1].Cells[0].Value.ToString();
this.textBox2.Text = (string)dataGridView1.Rows[index1].Cells[1].Value.ToString();
this.textBox3.Text = (string)dataGridView1.Rows[index1].Cells[2].Value.ToString();

//学习多种不同选中列表datagridview不同行的区别及选中不同行突出以其它色调显示
//selectionmode指示如何选择不同的单元格
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//defaultcellstyle为控制单元格的不同样式
//selectionbackcolor为选中单元格时的色调
//dataGridView1.DefaultCellStyle.SelectionBackColor = Color.;
}

//在列表datagridview中直接修改数据(添加删除操作)并马上同步到oracle数据库表中
private void button1_Click(object sender, EventArgs e)
{
//一定要用oraclecommandbuilder来封装下适配器
OracleCommandBuilder cx = new OracleCommandBuilder(da1);
//用适配器的update方法,update方法的参数为列表数据源datatable(利用适配器的fill填充生成)
da1.Update(dt1);


}




}
}