
Nguyên bản được gửi bởi
thienthan
Bạn không post code lên thì ai mà biết sai ở đâu mà giúp chứ?
Về nguyên tắc thì thấy chỗ nào sửa thì sửa nó ở trong CSDL thôi.
Code:
class DataUpdate
{
public string tablename;
public string query;
public SqlConnection cn;
public DataUpdate(SqlConnection cn, string query, string tablename)
{
this.cn = cn;
this.tablename = tablename;
this.query = query;
}
public DataSet Capnhat(DataGridView data)
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(query, cn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds, tablename);
DataTable table = ds.Tables[tablename];
for(int i=0;i<data.RowCount-1;i++)
for (int j = 0; j < data.ColumnCount; j++)
{
table.Rows[i][j] = data[j, i].Value.ToString();
}
builder.GetUpdateCommand();//bị lỗi ở đây
adapter.Update(ds, tablename);
return ds;
}
}
Cái lớp của mình đó, ý mình là thay đổi trên DataGridView thì nhấn nút Update thì CSDL cũng thay đổi.