MaterialSkin icon indicating copy to clipboard operation
MaterialSkin copied to clipboard

Combobox text property Not Working

Open talhayksk opened this issue 2 years ago • 10 comments

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

talhayksk avatar Mar 23 '22 19:03 talhayksk

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 🙂

valimaties avatar Mar 24 '22 07:03 valimaties

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();
          
   
        }

talhayksk avatar Mar 24 '22 10:03 talhayksk

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 avatar Mar 24 '22 12:03 valimaties

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

MaterialComboboxTextProperty

valimaties avatar Mar 24 '22 13:03 valimaties

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

valimaties avatar Mar 28 '22 06:03 valimaties

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

talhayksk avatar Mar 29 '22 06:03 talhayksk

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

valimaties avatar Mar 29 '22 14:03 valimaties

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

talhayksk avatar Mar 29 '22 17:03 talhayksk

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.

valimaties avatar Mar 29 '22 19:03 valimaties

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.

talhayksk avatar Sep 03 '22 21:09 talhayksk