简体   繁体   English

如何在Windows窗体应用程序(C#)中更改在文本上而不是在值上展开的选定组合框项目?

[英]How to change selected combobox item depanding on text instead of value in windows form application (C#)?

I am developing a windows form application where I am trying to edit data of selected row in a Datagridview using textbox and combobox (dropdown) in windows form application. 我正在开发Windows窗体应用程序,在其中尝试使用Windows窗体应用程序中的文本框和组合框(下拉列表)在Datagridview中编辑所选行的数据。 There is a column called status in Datagridview whose value is text (eg status = "start"). Datagridview中有一个名为status的列,其值是文本(例如status =“ start”)。 I need to change the prepopulate combobox depanding on the status column of selected row in the Datagridview. 我需要更改Datagridview中选定行的状态列上的预填充组合框。

I am getting null value in selectedvalue of combobox 我在组合框的selectedValue中得到了空值

combobox1.SelectedValue = dataGridView1.Rows[rowIndex].Cells["Status"].Value.ToString();

Where 哪里

dataGridView1.Rows[rowIndex].Cells["Status"].Value.ToString() = "Start"

Is there a way to change the selected combobox option depanding on text instead of value in windows form application using C#? 有没有办法更改使用C#在Windows窗体应用程序中在文本上而不是在值上更改所选组合框选项的显示方式?

If I'm understanding your question correctly, you have two columns inside a DataGridView: one containing text and one containing combo boxes, and you want the selected combo box element to change with the text. 如果我正确理解了您的问题,则DataGridView内有两列:一列包含文本,一列包含组合框,并且您希望所选组合框元素随文本更改。

I can't be overly specific because you haven't posted any code, but to get you started, you'll probably want to listen for a change in the text box column using an event handler: 我不能说得太具体,因为您还没有发布任何代码,但是为了入门,您可能需要使用事件处理程序来监听文本框列中的更改:

myDataGridView.CellClick += myDataGridView_CellClick;

...

void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex != TEXTBOX_COLUMN_ID) return;
    object value = myDataGridView[e.ColumnIndex, e.RowIndex].Value;
}

The local variable value will then have whatever was entered into the textbox column. 然后,本地变量值将具有在文本框列中输入的任何内容。 From there, you'll need to find the dropdown box in the same row (using e.RowIndex) and adjust its value however you wish. 从那里,您需要在同一行中找到下拉框(使用e.RowIndex)并根据需要调整其值。

On the "CellClick" event, maybe you should feed in the row into a DataGridViewRow object, then say if that cell within the row selected contains "Start", run a function that populates the combobox? 在“ CellClick”事件上,也许您应该将行输入到DataGridViewRow对象中,然后说是否所选行中的该单元格包含“开始”,运行填充组合框的函数?

ex: 例如:

        DataGridViewRow row = dataGridView1.Rows[rowIndex];
        if (row.Cells["Status"].Value.ToString() == "Start")
        {
            //fill combobox with needed information
        }

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

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