我有这个问题;
int gender = int.Parse(row.Cells [0] .Value.ToString()); cmbGender.SelectedValue =性别;
答案 0 :(得分:0)
很难帮助您,因为您没有以有条理的方式发布代码的所有相关部分。
我看到您是新来的,请阅读:How do I ask a good question。
< / p>
因为你的问题中缺少部分我会作出一些假设 请尝试了解我的答案的原则。
Datatable
来从数据库中检索数据。我建议使用字典以获取每个Cellclick
事件中的ID的性别
请阅读代码中的注释:
private void Form1_Load(object sender, EventArgs e)
{
GetDataFromDataBase();
}
Dictionary<int, int> IdGenderDict;
private void GetDataFromDataBase()
{
// get data from database
DataTable dt = DAL.DataBase.GetData();
// set datagridview
dtGrdAdd.DataSource = dt;
// initilize dictionary that willl hold key value pair for ID against its gender
IdGenderDict = new Dictionary<int, int>();
// set the dictionary
for (int i = 0; i < dt.Rows.Count; i++)
{
IdGenderDict.Add(Convert.ToInt32(dt.Rows[i]["ID"].ToString()),
Convert.ToInt32(dt.Rows[i]["Gender"].ToString()));
}
}
private void dtGrdAdd_CellClick(object sender, DataGridViewCellEventArgs e)
{
// get the selected id from the datagridview
var id = Convert.ToInt32(this.dtGrdAdd.Rows[e.RowIndex].Cells["ID"].ToString());
// find the gender using the dictionary
var gender = IdGenderDict[id];
// set the combobox with the gender
if (gender == 0)
{
cmbGender.SelectedItem = "MALE";
}
else
{
cmbGender.SelectedItem = "FEMALE";
}
}
答案 1 :(得分:0)
从Databse到storedProcedure: Gender.ID作为GenderID, Gender.Gender,
INNER JOIN Gender ON Gender.ID = Staff.Gender
在WinForms中:
private void dtGrdAdd_CellClick(object sender,DataGridViewCellEventArgs e)
{
//获取包含所有行的集合
DataGridViewRow row = this.dtGrdAdd.Rows[e.RowIndex];
// row.Cells [4] .Value表示DataGridView上Row的索引
int gender = int.Parse(row.Cells[4].Value.ToString());
cmbGender.SelectedValue = gender;
}