简体   繁体   中英

Bind to Combo box based on Data Gridview selection

I have this problem;

int gender = int.Parse(row.Cells[0].Value.ToString()); cmbGender.SelectedValue = gender;

  1. I saved values to a different table in my database using the ID of values from another table.
  2. I want to retrieve the name instead of the ID of the value from my database and bind to a combo box. The combo Box already has selections from the Database.
  3. The code above gives me female for the first selection from my data gridview into the combo box. And male is given for the second selection.
  4. When I move to the third row in my table, no gender is selected.
  5. The first selection does not correspond with the exact record in the database either. I need your help

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 .

  1. I assume you use Datatable in order to retrieve data from database.
  2. I assume that column names are "ID" and "Gender"
  3. I assume that Gender has the values of 0 (male) and 1 ("female").
  4. I assume that your combobox values are "MALE" and "FEMALE"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM