使用不同的DataSource,ValueMember和DisplayMember填充DataGridViewComboBox列 [英] Populate DataGridViewComboBox Column with diferent DataSource, ValueMember and DisplayMember

查看:147
本文介绍了使用不同的DataSource,ValueMember和DisplayMember填充DataGridViewComboBox列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,



这让我很生气:(我正在尝试使用不同的DataSource,ValueMember和DisplayMember根据其他列值填充DataGridViewComboBox列同一行。

我现在使用的代码是:



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







问题在于具有绑定控件的行是ComboBox(这意味着填充的cboCell具有DataSource,ValueMember和DisplayMember)仅显示VALUEMEMBER :(仅当我单击或编辑该行的单元格时,单元格填充它应该如何显示并显示DisplayMembers。然后第二个问题出现之后,同一个Cell只显示DisplayMembers并且还保存到绑定的SQLTable中的Cell只有显示成员。



我如何制作它总是显示DisplayMembers并保存ValueMembers。



PS

在EditingControlShowing中制作一些东西没有帮助所以mutch :(




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 :(

推荐答案

看你可以按照以下方式编辑它



See You can edit it in following way

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


问题是DataGridView不是Bount到Source,我添加了这样的行:



The Problem was that the DataGridView was not Bount to an Source and I was adding rows like this:

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





因此,ComboBox Cell的价值就像ValueMember而不是DispleyMember。也正因为如此,ComboBox在我使用ComboBox之后就像它一样工作。解决方案应该是创建Temporal DataSet / DataTable,将参数值添加到该DataSet,然后将DataSet绑定到DataGridView。



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.


这篇关于使用不同的DataSource,ValueMember和DisplayMember填充DataGridViewComboBox列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆