using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataBind(); //调用数据绑定,初始化数据
}
protected void DataBind()
{
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select * from persons where age>3 order by age asc", con);
sda.SelectCommand = cmd;
sda.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0]; //把第一个表设置为数据源
}
}
}
int id =Convert.ToInt32(this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value); //获取行ID号
然后组织SQL语句就OK了。
用datagridview更新数据库 (2008-04-30 12:59:38) 标签:datagridview 数据库 更新 it
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection ("server=192.168.1.122;uid=sa;pwd=sa;database=goods");
connection.Open();
SqlCommand mycomm = new SqlCommand("select * from DepartMent", connection);
adapter.SelectCommand = mycomm;
adapter.Fill(customers);
this.dataGridView1.DataSource = customers; //设置数据源
}
SqlCommandBuilder mybuikder = new SqlCommandBuilder(adapter);
adapter.Update(customers);