Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combobox text property Not Working #348

Closed
talhayksk opened this issue Mar 23, 2022 · 10 comments
Closed

Combobox text property Not Working #348

talhayksk opened this issue Mar 23, 2022 · 10 comments

Comments

@talhayksk
Copy link

hello, I want to add data to Combobox text property but I can't set it. (materialCombobox1.Text="example") this method doesn't work.

@valimaties
Copy link

I suggest using List<string> as DataSource for your MaterialCombobox object. Then use SelectedValue to get its value, but check that its SelectedIndex value to be greater than -1, else the value is string.Empty.

private List<string> CmbDataSource;
private string ComboSelectedValue { get; set; }

// your other code
// This method you will call in your class constructor, or anywhere else
private void SetComboboxDataSource()
{
    CmbDataSource = new List<string>();
    CmbDataSource.Add("Some First Value");
    CmbDataSource.Add("Some Second Value");
    CmbMaterialCombobox.DataSource = CmbDataSource;
}

// Other code here

private void CmbMaterialCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (CmbMaterialCombobox.SelectedIndex == -1)
    {
        ComboSelectedValue = string.Empty;
        return;
    }
    ComboSelectedValue = CmbMaterialCombobox.SelectedValue.ToString();
    // here you can do other things with this selected value
}

PS: I'm not on a computer with VS, so I didn't test this code, I hope it is written well 🙂

@talhayksk
Copy link
Author

thank you,
I'm already loading it this way, but then I want the name field to appear from the row with the combobox text set selected in the DataGridView cells_click event, but it doesn't set.

      public static async Task Parti(ComboBox comboBox)
        {
            var data= await _employee.GetAll();
            data.Insert(0, new Employee{ Name= "Select", Id = 0 });
            comboBox.DataSource = data;
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "Id";

        }

I do it this way, I tried other methods, but I didn't get the request.
textbox also happens with this method

        private void datagridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            matCombobox.Text =datagridView.CurrentRow.Cells[2].Value.ToString();
          
   
        }

@valimaties
Copy link

valimaties commented Mar 24, 2022

var selValue = (materialComboBox1.SelectedItem as Employee)?.Name; // here is your record from your cell's grid
            var find = ((List<Employee>)materialComboBox2.DataSource).AsEnumerable().Where(r => r.Name.Equals(selValue)).First();
            materialComboBox2.SelectedItem = find;

I think is more simple to create a DataTable which will be a DataSource for a BindingSource. Set this BindingSource as DataSource for your Combobox. Now it is more easy to use BindingSource.Find() method in your grid's CellClick method, moving in bindingsource to found position (using BindingSource.Position = found). Give it a try.

@valimaties
Copy link

And, BTW, I made some tests now, and Text property works...

MaterialComboboxTextProperty

@valimaties
Copy link

@talhayksk , some news about this issue? Did you managed to make it work on your side? If so, you may close this issue.

@talhayksk
Copy link
Author

talhayksk commented Mar 29, 2022

I am sorry,Unfortunately, it did not happen
it works as you did but it doesn't work on datagridview cellsClick event :(

@valimaties
Copy link

I will make some tests on my side with datagridview and I will come back with a feedback.

@talhayksk
Copy link
Author

Thank you very much for your attention. I will be waiting

@valimaties
Copy link

One more question, to understand what exactly do you want to do there:
When you set the value from datagirdview's cell to combobox, the value of that cell exists in combobox datasource?
I don't understand what do you want to do in your application... Something is wrong there, IMO... please tell me more...

BTW, I checked on my side and if the value of the cell exists in combobox datasource, then it will change the SelectedValue to me (also displays correctly the value of combobox).

As a checking option, add this code Debug.WriteLine($"Cell value is {datagridView.CurrentRow.Cells[2].Value}") in your CellClick event and see in debugger what is the value of the cell when you clicked that cell... I think the value from the cell is not existing in your combobox's datasource.

@talhayksk
Copy link
Author

Hi @valimaties , first of all, I'm sorry, I couldn't get back to you because of the issues that developed out of my will for a long time. I solved the problem in line with the information you gave, thank you, in this case, we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants