简体   繁体   English

根据数据网格视图选择绑定到组合框

[英]Bind to Combo box based on Data Gridview selection

I have this problem; 我有这个问题;

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

  1. I saved values to a different table in my database using the ID of values from another table. 我使用另一个表中的值ID将值保存到数据库中的另一个表中。
  2. I want to retrieve the name instead of the ID of the value from my database and bind to a combo box. 我想从数据库中检索名称而不是值的ID并将其绑定到组合框。 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. 我假设您使用Datatable以便从数据库检索数据。
  2. I assume that column names are "ID" and "Gender" 我假设列名是“ ID”和“性别”
  3. I assume that Gender has the values of 0 (male) and 1 ("female"). 我假设性别的值为0(男性)和1(“女性”)。
  4. I assume that your combobox values are "MALE" and "FEMALE" 我假设您的组合框值为“ MALE”和“ FEMALE”

i suggest to use dictionary in order to get the gender of the ID in each Cellclick event 我建议使用字典以获取每个Cellclick事件中ID的性别

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, 从Databse通过storedProcedure:Gender.ID作为GenderID,Gender.Gender,

INNER JOIN Gender ON Gender.ID = Staff.Gender 内联性别ON Gender.ID = Staff.Gender

In WinForms: 在WinForms中:

private void dtGrdAdd_CellClick(object sender, DataGridViewCellEventArgs e) 私有void dtGrdAdd_CellClick(对象发送者,DataGridViewCellEventArgs e)
{ {

//gets a collection that contains all the rows DataGridViewRow row = this.dtGrdAdd.Rows[e.RowIndex]; //获取一个包含所有行的集合DataGridViewRow row = this.dtGrdAdd.Rows[e.RowIndex];

// row.Cells[4].Value represents the index of the Row on the DataGridView // row.Cells [4] .Value代表DataGridView上Row的索引
int gender = int.Parse(row.Cells[4].Value.ToString()); cmbGender.SelectedValue = gender;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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