简体   繁体   English

如何将值从Datagridview传递到c#中的组合框?

[英]how to pass value from Datagridview to combobox in c#?

I have Datagridview and combobox . 我有Datagridviewcombobox I need to send data from Datagridview to combobox (I need that for update that data and return it back to Datagridview). 我需要将数据从Datagridview发送到组合框 (我需要更新数据并将其返回给Datagridview)。

I'm using this code in DoubleClick event of Datagridview to get data from datagridview to combobox and another textbox and DateTimepicker: 我在Datagridview的DoubleClick事件中使用以下代码,将数据从datagridview获取到组合框和另一个文本框和DateTimepicker:

private void mgrid_searchClient_Contrat_DoubleClick(object sender, EventArgs e)
{
    mcb_NomClient.Text= this.mgrid_searchClient_Contrat.CurrentRow.Cells[0].Value.ToString();
    mtb_NomContrat.Text = this.mgrid_searchClient_Contrat.CurrentRow.Cells[1].Value.ToString();
    mdtp_DATEECHIANCE.Text = this.mgrid_searchClient_Contrat.CurrentRow.Cells[2].Value.ToString();    
}

mcb_NomClient is a ComboBox , mtb_NomContrat is a TextBox, and mdtp_DATEECHIANCE is DateTimePicker. mcb_NomClient是ComboBox, mtb_NomContrat是TextBox, mdtp_DATEECHIANCE是DateTimePicker。

If you want to show the double-clicked cell content to the ComboBox , you may use DataGridView.CurrentCell.Value , and ComboBox.Items.Add() as follows. 如果要将双击的单元格内容显示到ComboBox ,则可以如下使用DataGridView.CurrentCell.ValueComboBox.Items.Add()

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    var val = dataGridView1.CurrentCell.Value;
    comboBox1.Items.Add(val);
}

Doing so will only 'add' an item, but to show an item in the ComboBox you'll need to set the SelectedIndex as well. 这样做只会“添加”一个项目,但是要在ComboBox显示一个项目,您还需要设置SelectedIndex

To show the most-recently added item: 要显示最近添加的项目:

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    var val = dataGridView1.CurrentCell.Value;
    comboBox1.Items.Add(val);
    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
}

To show the first item: 要显示第一项:

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    var val = dataGridView1.CurrentCell.Value;
    comboBox1.Items.Add(val);
    comboBox1.SelectedIndex = 0;
}

EDIT (due to OP's updated requirements): 编辑(由于OP的更新要求):

Let's say your DataGridView has 3 columns, namely, ID , Name , and City . 假设您的DataGridView有3列,即IDNameCity And let us also say your ComboBox is populated with those Name values. 让我们也说您的ComboBox填充了这些Name值。 And upon double-clicking a DataGridView row (any cell in a particular row), you want to display in the ComboBox the Name value that matches the double-clicked row's Name . 双击DataGridView行(特定行中的任何单元格)后,您想要在ComboBox 显示与双击行的Name相匹配的Name值。

For example; 例如; DGV looks like this: DGV看起来像这样:

ID | ID | Name | 姓名| City


1 | 1 | Jane | 简| New York 纽约

2 | 2 | Tom | 汤姆| Melbourne 墨尔本

3 | 3 | Chelsea | 切尔西| London 伦敦

And your ComboBox has values Jane , Tom , and Chelsea . 而且您的ComboBox值包括JaneTomChelsea When you double click a row (any cell), you want to show that row's name. 当您双击一行(任何单元格)时,您想要显示该行的名称。 For instance, you double-click the cell London and you want the ComboBox to show Chelsea . 例如,双击London单元格,并希望ComboBox显示Chelsea

In that case you need to get the current row (clicked row), then get the Name column value in that row, and look it up in the ComboBox values. 在这种情况下,您需要获取当前行(单击的行),然后获取该行中的“ Name列值,然后在ComboBox值中查找它。

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    var currentRow = dataGridView1.CurrentRow;
    var selectedName = currentRow.Cells[1].Value;

    var index = comboBox1.Items.IndexOf(selectedName.ToString());
    comboBox1.SelectedIndex = index;
}

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

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