Home Forums WinForms controls Xceed Grid for WinForms ValueMember in bound ComboBox

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17515 |

    Hi,

    I have the .NET grid (not WPF) with a column that contains a combobox, that is bound to a datatable in a dataset.  

    I do the binding like this:

    void MyCombo_ActivatingControl(object sender, CellEditorEventArgs e)

    {

        WinComboBox myCombo = e.Control as WinComboBox;

        myCombo.SetDataBinding(GetMyDataSet(), “TheTable”);

        myCombo.DisplayFormat = “%Description%”;

        myCombo.ValueMember = “ID”;

    }

    So the display works fine: I see the Description field of the “TheTable” table displayed as expected. My question is, shouldn’t the Value of this cell now be the “ID” in the bound table, rather than the Description? Isn’t that the whole point of the ValueMember field?

    If for example, I need to get the currently selected Value in this combobox, from some other part of the code, I have to do this:

    … 

    row.Cells[“TheColumnWithTheComboBox”].EnterEdit();

    WinComboBox MyCombo = row.Cells[“TheColumnWithTheComboBox”].CellEditorControl as WinComboBox; 

     …

    If I now do MyCombo.SelectedValue or indeed just:

    row.Cells[“TheColumnWithTheComboBox”].Value.ToString()

    I would expect the Value to be the “ID” column of the bound dataTable, not the description which is what it is?

    So how do I get the ID value? 

     

    Thanks

    Nigel 

    Imported from legacy forums. Posted by Nigel (had 4487 views)

    User (Old forums)
    Member
    Post count: 23064

    Additionally – if I set the AllowFreeText value to False, I get this exception intermittently:

    “When AllowFreeText is set to False, the text can only be set to an empty string or to a matching ComboBoxItem, formatted using the current DisplayFormat.”

     Followed by several more:

    “The text could not be validated.”

    “The RawText is invalid.”

    “Parameter name: value”

     “The current value of the cell cannot be handled by the associated cell editor.”

    “The cell’s editor could not be activated.”

    What is the cause of this please?

      

    Imported from legacy forums. Posted by Nigel (had 399 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Nigel, 

     Would it be possible to send us a self-contained sample project which demonstrates this issue at support@xceed.com, and include a link to this forum thread for reference.

     

    Imported from legacy forums. Posted by Michel [Xceed] (had 255 views)

    User (Old forums)
    Member
    Post count: 23064

    Sorry one more question: I am in another part of code, where I have access to a particular row. I just want to set the value of the ComboBox in that row from a string.

    AFAIK the only way I can access the relevant combobox is like this:

     row.Cells[“TheColumnWithTheComboBox”].EnterEdit();

    WinComboBox MyCombo = row.Cells[“TheColumnWithTheComboBox”].CellEditorControl as WinComboBox;

     

    The EnterEdit call is causing an ArgumentException to be thrown:

    ” The specified cell cannot be the current cell because it does not belong to the grid. Parameter name: CurrentCell”

     What does this mean? Why can’t I just set the displayed value to be whatever I want?

    I also tried just setting directly:

     row.Cells[“TheColumnWithTheComboBox”].Value = newValue;

     which gets set OK, but it doesn’t get reflected on the screen, it still shows the first item of the combobox.

     EDIT: Setting Value works OK, problem was me. Still curious about the exception though.

    Imported from legacy forums. Posted by Nigel (had 520 views)

    Xceed Support
    Member
    Post count: 5658
    Hi,
     
    I presume you want a “Free text” ComboBox. By default the ComboBoxEditor is not in that mode; here is how you can set it:

    YourComboBoxEditor.TemplateControl.AllowFreeText = true;

    Imported from legacy forums. Posted by Alain [Xceed] (had 261 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,

    No, I want the combobox to be bound to a dataset, which is what I’ve done. I dont want it to be free text. But as I said, if I leave the default set to false, it throws the exception I mention.

    Setting to true seems to be the only way to prevent it. Do you know why the error is thrown? I kind of assumed you have access to the source code so could look up the error and decide if it’s something I’m doing or a fault with the grid.

    However, the first issue I raised is more important for me. Is anyone looking at that?

    thanks 

     

    Imported from legacy forums. Posted by Nigel (had 482 views)

    Xceed Support
    Member
    Post count: 5658
    Hi,
     
    I presume the cell that you want to set the ComboBox on already contains the “Key” for that ComboBox. 
     
    Yes, you are right for your question that setting the combobox as you mentioned will make it write back the “key” to the cell and not the description. If your cell contains the description from the beginning, then you should put the value member of the combo box to be the description also.

    Did you put any viewer on that column ? ( because from what I’m reading i’m not sure if you have used one or not )

    If you could provide a sample, It will help a lot to understand faster what exactly is your datasource.


    Imported from legacy forums. Posted by Alain [Xceed] (had 236 views)

    User (Old forums)
    Member
    Post count: 23064

    There is a CellEditorViewer, but only to set colouring. I tried not using it and it makes no difference.

    Ok, so I can’t send you a full sample as I explained, but here is all you should need:

     – Grid column is set like this:

     ComboBoxEditor myCombo = new ComboBoxEditor();

    myCombo.TemplateControl.DropDownControl.ScrollBars = GridScrollBars.None;

    height = gridBasketControl.Font.Height * 5;

    myCombo.TemplateControl.DropDownSize = new Size(gridBasketControl.Columns[“MyColumn”].Width, height);

    gridBasketControl.Columns[“MyColumn”].CellEditorManager = myCombo;

    – The databinding is different for each row, so I can’t set it at the template control level, so I set it on activating event:

     void MyCellEditorManager_ActivatingControl(object sender, CellEditorEventArgs e)

            {

                WinComboBox combo = e.Control as WinComboBox;

                combo.DisplayFormat = “%Key%”;

                combo.SetDataBinding(dataSet, “TableName”);

                combo.ValueMember = “Value”;

                combo.DropDownControl.Columns[“Value”].Visible = false;

            }

     

     – Here is the dataset I’m binding to, consisting of one table:

    DataSet dataSet = new DataSet();

    dataSet.Tables.Add(new DataTable(“TableName”));

    dataSet.Tables[“TableName”].Columns.Add(new DataColumn(“Key”));

    dataSet.Tables[“TableName”].Columns[0].AllowDBNull = false;

    dataSet.Tables[“TableName”].Columns[0].Unique = false;


    dataSet.Tables[“TableName”].Columns.Add(new DataColumn(“Value”));

    dataSet.Tables[“TableName”].Columns[1].AllowDBNull = false;

    dataSet.Tables[“TableName”].Columns[1].Unique = false;


    dataSet.Tables[“TableName”].Rows.Add(new object[] { “Key1”, “value1” });

    dataSet.Tables[“TableName”].Rows.Add(new object[] { “Key2”, “value2” });

     

    So the expected behaviour, is this:

    1) When you click on this column, you get the combox box showing “Key1” and “Key2”.

    2) When this cell loses focus, whatever was selected stays there!

    3) I am able to retrieve the *values* (value1 or value2) depending on what was selected.

     

    The actual behaviour is:

    1) Works fine.

    2) Once you click away, the VALUE is displayed! So eg value1 replaces Key1. Additionally, if I try and click back again, the combobox will not appear again and the activating event will never fire again.

    3) As per my initial post, I am unable to get the value programatically from within the code. I need to just get either value1 or value 2 as per what is selected.

     

    Thanks for your help in how to resolve these. 

     

     

     

     

    Imported from legacy forums. Posted by Nigel (had 322 views)

    User (Old forums)
    Member
    Post count: 23064

    OK, I have resolved most of this. I hadn’t realised you need to set a ComboBoxViewerManager as well as the Editor…would have been useful if the doc’s made this a bit clearer: http://doc.xceedsoft.com/products/Xceedgrid/ComboBoxEditor.html doesn’t mention this.

    I am now having to set both of these in the CurrentCellChanged event, rather than the Activating event which stops being fired once the data has been bounded strangely.

     

    I still have a problem though: I need to set the displayed value of the cell to be a value set from a string, before the user clicks on the cell and thus picks something from the combobox. I tried doing this:

    currentRow.Cells[“MyColumn”].Value = “Something”; 

    But this prevents the combo from ever appearing if the user clicks on the cell.

    How do I set the display value from just a string? This must be done from outside the GUI events, so not on Activating, CurrentCellChanged etc.

     

    Thanks 

    Imported from legacy forums. Posted by Nigel (had 395 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Nigel,

    It is not possible to set a default value on a ComboBoxViewer in a straight-forward way.

    You can create a custom Viewer by deriving from ComboBoxViewer; in your Viewer, you can test if the value is null (that is, an edit process did not yet occur), then display the text you want. However, this has to be done inside a GUI event.

    Imported from legacy forums. Posted by Alain [Xceed] (had 1970 views)

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.