![]() |
#2
haining66992016-05-14 20:31
|

using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace c1
{
public partial class Form1 : Form
{
private SqlConnection con;
private BindingManagerBase dh;
private SqlCommandBuilder dc;
private SqlDataAdapter dx;
private DataSet dq;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strcon = "server=localhost;uid=sa;pwd=123456;database=Test";
// using (SqlConnection con = new SqlConnection())
// {
con = new SqlConnection();
con.ConnectionString = strcon;
con.Open();
dx = new SqlDataAdapter("select * from dwxx", con);
dc = new SqlCommandBuilder(dx);
dq = new DataSet();
dx.Fill(dq, "dw");
Binding b1 = new Binding("Text", dq, "dw.单位编号");
textBox1.DataBindings.Add(b1);
Binding b2 = new Binding("Text", dq, "dw.单位名称");
textBox2.DataBindings.Add(b2);
dataGridView1.DataSource = dq.Tables["dw"];
dh=this.BindingContext[dq,"dw"];
comboBox1.DataSource=dq.Tables["dw"];
comboBox1.DisplayMember = "单位名称";
// }
}
private void button1_Click(object sender, EventArgs e)
{
dh.Position = 0;
}
private void button2_Click(object sender, EventArgs e)
{
if (dh.Position < dh.Count - 1) dh.Position++;
}
private void button3_Click(object sender, EventArgs e)
{
if (dh.Position > 0) dh.Position--;
}
private void button4_Click(object sender, EventArgs e)
{
dh.Position = dh.Count - 1;
}
private void button6_Click(object sender, EventArgs e)
{
dx.Update(dq, "dw");
}
}
}
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace c1
{
public partial class Form1 : Form
{
private SqlConnection con;
private BindingManagerBase dh;
private SqlCommandBuilder dc;
private SqlDataAdapter dx;
private DataSet dq;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strcon = "server=localhost;uid=sa;pwd=123456;database=Test";
// using (SqlConnection con = new SqlConnection())
// {
con = new SqlConnection();
con.ConnectionString = strcon;
con.Open();
dx = new SqlDataAdapter("select * from dwxx", con);
dc = new SqlCommandBuilder(dx);
dq = new DataSet();
dx.Fill(dq, "dw");
Binding b1 = new Binding("Text", dq, "dw.单位编号");
textBox1.DataBindings.Add(b1);
Binding b2 = new Binding("Text", dq, "dw.单位名称");
textBox2.DataBindings.Add(b2);
dataGridView1.DataSource = dq.Tables["dw"];
dh=this.BindingContext[dq,"dw"];
comboBox1.DataSource=dq.Tables["dw"];
comboBox1.DisplayMember = "单位名称";
// }
}
private void button1_Click(object sender, EventArgs e)
{
dh.Position = 0;
}
private void button2_Click(object sender, EventArgs e)
{
if (dh.Position < dh.Count - 1) dh.Position++;
}
private void button3_Click(object sender, EventArgs e)
{
if (dh.Position > 0) dh.Position--;
}
private void button4_Click(object sender, EventArgs e)
{
dh.Position = dh.Count - 1;
}
private void button6_Click(object sender, EventArgs e)
{
dx.Update(dq, "dw");
}
}
}