【问题标题】:Datagridview gender value to comboboxDatagridview 性别值到组合框
【发布时间】:2015-05-28 09:29:30
【问题描述】:

当我点击它时,我希望 datagridview 值出现在“新条目表单”(文本框和组合框)上。

这是我的代码,它只适用于文本框,因为我不知道如何将 GENDER 值“从”datagridview 中返回到组合框

我的组合框值是“M”和“F”,我知道如何将它们输入到 datagridview 但反过来就是问题。

private void dataGridViewKontakti_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        lblID.Visible = true;
        lblIDPromjena.Visible = true;

        int i;
        i = dataGridViewKontakti.SelectedCells[0].RowIndex;

        lblIDPromjena.Text = dataGridViewKontakti.Rows[i].Cells[0].Value.ToString();
        textBoxIme.Text = dataGridViewKontakti.Rows[i].Cells[1].Value.ToString();
        textBoxPrezime.Text = dataGridViewKontakti.Rows[i].Cells[2].Value.ToString();
        textBoxZvanje.Text = dataGridViewKontakti.Rows[i].Cells[3].Value.ToString();
        textBoxZanimanje.Text = dataGridViewKontakti.Rows[i].Cells[4].Value.ToString();

        textBoxTelefon.Text = dataGridViewKontakti.Rows[i].Cells[6].Value.ToString();
        textBoxAdresa.Text = dataGridViewKontakti.Rows[i].Cells[7].Value.ToString();
        textBoxPost.Text = dataGridViewKontakti.Rows[i].Cells[8].Value.ToString();
        textBoxGrad.Text = dataGridViewKontakti.Rows[i].Cells[9].Value.ToString();
        textBoxEmail.Text = dataGridViewKontakti.Rows[i].Cells[10].Value.ToString();

您会看到单元格 4 和 6 之间的空白区域。单元格 5 是我的 GENDER 单元格。

【问题讨论】:

  • 我不太确定你在这里做什么/问什么。如果您从 DataGridView 中提取值,请向我们展示您期望的值、收到的值,以及如果您遇到异常,发生的位置
  • 我正在使用他的姓名、姓氏、职务、工作、性别(组合框值为 M 或 F)、电话、地址、邮局、城市和电子邮件注册新用户。我需要做的是当我点击其中的一行时,从 datagridview 获取值会出现在我的文本框中(但也在组合框中)。我编写的代码将值“返回”到文本框中,但是当我单击一行时,我还需要我的 GENDER 值(M 或 F)出现在我的组合框中(我不知道该怎么做)
  • 为了让值显示在组合框中,您需要在该组合框中有一个项目列表。创建组合框的代码在哪里?您是否设置了值/显示成员?在您的代码中,您从不访问值 5?你有没有试过看看会发生什么?你能告诉我们发生了什么吗?除非您向我们展示您的应用程序的更多代码,否则我无法为您提供帮助
  • //在我的组合框中添加了 2 个项目 cbSpol.Items.Add("M"); cbSpol.Items.Add("F"); //转换成字符串 private void cbSpol_SelectedIndexChanged(object sender, EventArgs e) { Convert.ToString(cbSpol.SelectedItem); } //插入带有 kSpol 的字符串,用于从组合框中输入 GENDER。字符串查询=“插入到dbo.kontakti(kIme,kPrezime,kSpol,kTelefon,kAdresa,kPost,kGrad,kEmail)值(kIme,kPrezime,kZvanje,kZanimanje,kSpol,......”); ... command.Parameters.AddWithValue("kSpol", cbSpol.SelectedItem); ...(参数旁边有@)

标签: c# datagridview combobox


【解决方案1】:

根据您的 cmets,我认为您需要在 ComboBox 上设置 SelectedItem,假设您的数据中包含字符串“M”和“F”:

    public Form1()
    {
        InitializeComponent();
        this.comboBox1.Items.Add("M");
        this.comboBox1.Items.Add("F");

        comboBox1.SelectedItem = "M";

    }

【讨论】:

  • 是的,但我已经用这个
     cbSpol.Items.Add("M"); cbSpol.Items.Add("F");  我没有写 public form() ,所以因为我正在失去评论空间。我需要这样的东西: <pre> cbSpol.SelectedItem = dataGridViewKontakti.Rows[i].Cells[5].Value.ToString();  这是我需要的行,但它不起作用,我想知道为什么?我是从头开始写的,我知道这行不通。当我单击(例如)datagrid 中的第三行时,我将所有值返回到 TEXTBOXES,但组合框没有从 datagrid 接收值(它保持空白)
  • 这一行dataGridViewKontakti.Rows[i].Cells[5].Value的值是多少
  • 我只是尝试将 cbSpol.SelectedItem 更改为 cbSpolText 并且它可以工作.... datagridview 将其性别值返回到 GENDER COMBOBOX 并且它现在可以工作了!但我仍然不明白为什么组合框不需要 SelectedItem 而不是 .Text 无论如何......它现在已修复。 @Matthew 非常感谢。我是新用户,所以我不能给你正面评价,但如果可以的话,我会立即完成。再次感谢您的麻烦!
猜你喜欢
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2014-09-11
  • 1970-01-01
  • 2021-02-20
  • 2017-06-09
  • 2012-12-12
相关资源
最近更新 更多