Click here to Skip to main content
15,124,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hy,

It's making me mad :( I´m trying to populate a DataGridViewComboBox Column with different DataSource, ValueMember and DisplayMember according to a other Column Value in the same Row.
The code I´m using right now is:

private void dgvFilter_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    //Get the Row that is Added
    DataGridViewRow row = dgvFilter.Rows[e.RowIndex];
    //Get the Column/Cell with the ComboBox to be populated
    DataGridViewCell cell = row.Cells["Filter"];
    //Cast the Column as an DataGridViewComboBoxCell
    DataGridViewComboBoxCell cboCell = cell as DataGridViewComboBoxCell;
    //Here I use some Classes to get the Control that is bound to the Value of the Column "Field"
    Control fieldControl = UsedTable.Columns[row.Cells["Feld"].Value.ToString()].Control;

    cboCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

    //If the bount Control is not set(is Null) or is an TextBox i just populate the cboCell with an String Array generated in a other Method (this works fine)
    //If the bound Control is a ComboBox I just want to reuse the DataSource, ValueMember and DisplayMember of that Control what also works
    if (fieldControl == null || fieldControl is TextBox)
    {
        cboCell.DataSource = DataStringSource(row.Cells["Feld"].Value.ToString());
    }
    else if (fieldControl is ComboBox)
    {
        ComboBox usedCbo = fieldControl as ComboBox;
        cboCell.DataSource = usedCbo.DataSource;
        cboCell.ValueMember = usedCbo.ValueMember;
        cboCell.DisplayMember = usedCbo.DisplayMember;
    }
}



The Problem is that the Row that has an bound Control that is an ComboBox (witch means that the populated cboCell has DataSource, ValueMember and DisplayMember) shows only the VALUEMEMBER :( only if i Click or Edit the Cell of that Row the Cell populates how it should and shows the DisplayMembers. Then a Second Problem acures After that the same Cell shows only the DisplayMembers and also Saves to the Cell in the bound SQLTable only the Displaymember.

How Can i make it that the DisplayMembers are always shown and the ValueMembers Saved.

P.S.
Making something in the EditingControlShowing didn´t help so mutch :(
Posted
Updated 22-Apr-14 11:14am
v2

See You can edit it in following way

C#
else if (fieldControl is ComboBox)
           {
               ComboBox usedCbo = fieldControl as ComboBox;
               cboCell.DataSource = usedCbo.DataSource;
               cboCell.ValueMember = "ColumnNameLikeID";
               cboCell.DisplayMember = "DisplayNameLikeFirstName";
           }


After Populate when you will select one item you need to save it's value member to database

Then try to fine it's selected value
int id=Convert.ToInt32(cboCell.SelectedValue);
   
Comments
TarikHuber 23-Apr-14 6:14am
   
Thank you for your Answer.

The first Code


else if (fieldControl is ComboBox)
{
ComboBox usedCbo = fieldControl as ComboBox;
cboCell.DataSource = usedCbo.DataSource;
cboCell.ValueMember = "ColumnNameLikeID";
cboCell.DisplayMember = "DisplayNameLikeFirstName";
}


Is the Same as mine. Because usedCbo.ValueMember as an Value like "ID" and usedCbo.DisplayMember a Value Like "Name". Because this DataGridView is used to Filter an Other DataGridView on withc I don't know the Exact Values of the ValueMember and DisplayMember. So can´t HardCode it. Aniway the ComboBox is Populated with the right ValuesMember and DisplayMember. The Problem is that it shows only the Value Member UNTIL I select a other Value. After I select an other Value in the ComboBox everithing is working fine. Also the changed Value is saved as Cell.Value , the saved DisplayValue s saved as Cell.Text.

Because I dont save the DataGrid to an Databes. It shows the Values of my Filter Parameters. On CellValidated Event i Save the changes to the Prameters like this:



private void dgvFilter_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dgvFilter.Columns["Filter"].Index)
{
DataGridViewCell cell = dgvFilter.Rows[e.RowIndex].Cells[e.ColumnIndex];
var value = cell.Value;
string columnName = dgvFilter.Rows[e.RowIndex].Cells["Feld"].Value.ToString();
Column column = UsedTable.Columns[columnName];
UsedTable.ChangeParameter(column, value);
}
}


To be Clear with the problem I explain it in short: The DataGridViewComboBoxCell shows the DisplayMamber (and works like it should) only after I select an other Value in it. Befor that it shows only te Value member.
[no name] 23-Apr-14 6:21am
   
You should give name as your database table column name.
TarikHuber 23-Apr-14 7:27am
   
That is also alright. But I thing I have the solution ;)
[no name] 23-Apr-14 8:23am
   
yes you can do it
The Problem was that the DataGridView was not Bount to an Source and I was adding rows like this:

C#
private void LoadTableItems()
     {
         dgvFilter.Rows.Clear();
         foreach (KeyValuePair<int,> param in UsedTable.Parameters)
         {
             string value = param.Value.Value.ToString();
             string name = param.Value.Column.Name;
             string descriptionName = param.Value.Column.HeaderText;
             dgvFilter.Rows.Add(param.Key, param.Value.ConditionalOperator.ToString(),
                 name, descriptionName, param.Value.RationalOperator.ToString(),
                 value.Replace("%", ""));
         }
     }


Because of that the Value of the ComboBox Cell is interprated like the ValueMember and not as DispleyMember. Also because of that the ComboBox works like it sould after I work with the ComboBox. The Solution should be to make a Temporal DataSet/DataTable, add the Parameter Values to that DataSet and then bind the DataSet to the DataGridView.
   
Comments
TarikHuber 23-Apr-14 8:35am
   
I tried it out with an Temporal DataTable witch I bind to the DataGrid but it doesnt help :( it´s still showing only the ValueMber until I start to edit the cell. But now it´s worse. They Cell stays showing only the ValueMember :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900