dataGridView将数据显示到textBox
在windows下面.microsoft改造了ado成ado.net.大力改的控件当然是recordset,在ado年代那个时候的数据库操作全是连接式的操作,到了web年代microsoft把ado的recordset断开,成为ado.net下面的dataset和..dataadapter,这样一下把生成的结果集经过适配器的改成数据集(形式于数据库),断开后填充dataset,再用控件datagrid/datagridview显示出来.改造后的两个控件(dataset/dataadapter)才等于recordset,其实不等(嘿嘿,只是方便理解).其实连接式的数据操作还是用sqlconnection/sqlcommand完成的.下面讲的当是将datagridview中的当前显示的数据用文本显示出来,这对编写数据库软件有相当大的用处,平时如果查询增加当然没多大关系,用sqlconnection/sqlcommand完成.更新的时候和删除的时候不能主观上显示肯定麻烦.进入主题
数据库字段id,name,password
先发一第效果图
![](https://www.cnblogs.com/images/cnblogs_com/izxp/111.jpg)
不多说直接给出代码
private void Form1_Load(object sender, EventArgs e)
{
//断开式连接数据库
SqlDataAdapter myDataAapter = new SqlDataAdapter("Select * From users", "data source=localhost;database=test;user id=sa;pwd=");
DataSet myDataSet = new DataSet();
//填充dataset映射表users
myDataAapter.Fill(myDataSet, "users");
dataGridView1.DataSource = myDataSet.Tables["users"];
//初始化赋第一行值
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["name"].Value.ToString();
textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["password"].Value.ToString();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//单击某一单元格获取当前的行赋于字段值
textBox1.Text =dataGridView1.Rows[dataGridView1.CurrentRow.Index ].Cells["id"].Value.ToString() ;
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["name"].Value.ToString();
textBox3.Text=dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["password"].Value.ToString();
}
数据库字段id,name,password
先发一第效果图
![](https://www.cnblogs.com/images/cnblogs_com/izxp/111.jpg)
不多说直接给出代码
private void Form1_Load(object sender, EventArgs e)
{
//断开式连接数据库
SqlDataAdapter myDataAapter = new SqlDataAdapter("Select * From users", "data source=localhost;database=test;user id=sa;pwd=");
DataSet myDataSet = new DataSet();
//填充dataset映射表users
myDataAapter.Fill(myDataSet, "users");
dataGridView1.DataSource = myDataSet.Tables["users"];
//初始化赋第一行值
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["name"].Value.ToString();
textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["password"].Value.ToString();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//单击某一单元格获取当前的行赋于字段值
textBox1.Text =dataGridView1.Rows[dataGridView1.CurrentRow.Index ].Cells["id"].Value.ToString() ;
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["name"].Value.ToString();
textBox3.Text=dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["password"].Value.ToString();
}
Copy Right By Robo_zou