I have this problem;
int gender = int.Parse(row.Cells[0].Value.ToString()); cmbGender.SelectedValue = gender;
Its quite hard to help you because you did not publish all the relevant parts of your code in an organized manner.
i see you are new here please read: How do I ask a good question .
because there are missing parts in your question i will make some assumptions.
please try to understand the principals of my answer .
Datatable
in order to retrieve data from database. i suggest to use dictionary in order to get the gender of the ID in each Cellclick
event
please read comments inside the code:
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";
}
}
From Databse via storedProcedure: Gender.ID AS GenderID, Gender.Gender,
INNER JOIN Gender ON Gender.ID = Staff.Gender
In WinForms:
private void dtGrdAdd_CellClick(object sender, DataGridViewCellEventArgs e)
{
//gets a collection that contains all the rows DataGridViewRow row = this.dtGrdAdd.Rows[e.RowIndex];
// row.Cells[4].Value represents the index of the Row on the DataGridView
int gender = int.Parse(row.Cells[4].Value.ToString()); cmbGender.SelectedValue = gender;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.