C# DataGridView 添加、修改、删除数据实现
本帖最后由 wyz365889 于 2012-10-19 10:34:36 编辑
现在遇到的问题是怎么添加、删除一行数据到DatagridView显示并同步更新到数据库。
代码如下:
public partial class Form_User : Form
{
private DataOpt dataOpt;
private SqlCommand sqlCmd;
private SqlDataAdapter sqlDA;
private DataSet ds;
private DataTable dt;
public Form_User()
{
InitializeComponent();
dataOpt = new DataOpt();
dataOpt.SQLConn();
string strSql = "select * from T_User";
sqlCmd = new SqlCommand(strSql, dataOpt.conn);
sqlDA = new SqlDataAdapter(sqlCmd);
ds = new DataSet();
sqlDA.Fill(ds,"T_User");
dt = ds.Tables["T_User"];
this.dataGridView_user.DataSource = ds;
this.dataGridView_user.DataMember = "T_User";
this.dataGridView_user.AllowUserToAddRows = false;
this.dataGridView_user.EditMode = DataGridViewEditMode.EditProgrammatically;
this.dataGridView_user.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView_user.Columns[1].Visible = false;
//this.dataGridView_user.ColumnHeadersVisible = false;