- 填充DataTable,并用它作为一个比南源的DataGridView
1.1不要在datagridview的变化...
public void DAL_UpdateStudentsTable(DataTable table) //DAL represents 3-tyer architecture (so data access layer)
{
using (SqlConnection sqlConn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = @"UPDATE Students SET " +
"StudentID = @id, " +
"FirstName = @first, " +
"LastName = @last, " +
"Birthday = @birthday, " +
"PersonalNo = @personal " +
"WHERE StudentID = @oldId";
cmd.Parameters.Add("@id", SqlDbType.Int, 5, "StudentID");
cmd.Parameters.Add("@first", SqlDbType.VarChar, 50, "FirstName");
cmd.Parameters.Add("@last", SqlDbType.VarChar, 50, "LastName");
cmd.Parameters.Add("@birthday", SqlDbType.DateTime, 1, "Birthday");
cmd.Parameters.Add("@personal", SqlDbType.VarChar, 50, "PersonalNo");
SqlParameter param = cmd.Parameters.Add("@oldId", SqlDbType.Int, 5, "StudentID");
param.SourceVersion = DataRowVersion.Original;
cmd.Connection = sqlConn;
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.UpdateCommand = cmd;
da.Update(table);
}
}
}
}
- 当你想更新数据库只需创建一个更新命令,并像上面所看到的那样执行它。
谢谢。对于列表这个根本不是IBindingList实现的数据源,是否意味着AllowUserToAddRows或AllowUserToDeleteRows属性足以确定是否允许添加或删除行?列表似乎支持添加除了您的结论中的编辑和删除。谢谢 –
编辑'DataGridView'中'List'的唯一方法是使用'BindingSource'。根据绑定源规则,如果您将'List '设置为'BindingSource'的数据源,然后将'BindingSource'设置为'DataGridView'的数据源,那么您可以*添加*,*编辑*和*删除*清单'的项目。 –
除了上述支持* Add *的条件之外,别忘了使用公共无参数的构造函数(通常在普通类中有这个)。 –