我正在使用 C# [C sharp] 和 sql server 开发桌面应用程序。我有三张桌子:
tblSbjEmp
sID
eIDtblSbj
sID
SubjectNametblEmp
eID
员工姓名
我想在 Datagridview中显示tblSbjEmp 。我已经很容易在datagridview中替换了隐藏eID的组合框,但我的问题是我想要SubjectName(不是组合框)代替网格中的sID,它必须是只读的
public DataSet getDS(String strTableName, String strQuery)
{
myCon = new SqlConnection("connection string");
try
{
DS = new DataSet();
DA = new SqlDataAdapter(strQuery, myCon);
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(DA);
DA.Fill(DS, strTableName);
return DS;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "-----" + ex.ToString());
DataTable DT = new DataTable();
return DS;
}
finally
{
if (myCon.State == ConnectionState.Open)
myCon.Close();
}
}
ds = SObj.getDS("tblSbjEmp", "Select * from tblSbjEmp");
//Lecturer Combobox
DataGridViewComboBoxColumn Employee = new DataGridViewComboBoxColumn();
Employee.HeaderText = "Lecturer";
Employee.Name = "Employee";
Employee.MaxDropDownItems = 4;
Employee.Visible = true;
Employee.Width = 150;
Employee.DataSource = clsSettings.getAllRecords("Select eID, EmployeeName from tblEmp");
Employee.DataPropertyName = "eID";
Employee.ValueMember = "eID";
Employee.DisplayMember = "EmployeeName";
dgvLecturer.Columns.Add(Employee);
在组合框中,我们可以设置 DatapropertyName、ValueMember 和 DisplayMember 并选择组合框的数据源,但我怎样才能获得SubjectName(固定值)来代替 sID。但是当我必须更新表时,我必须更新 sID 以下是我使用的更新命令。SObj.DA.Update(ds, "tblSbjCrs"); 查询不能通过这种方法更新,所以我在网格后面使用了单个表。