将 DataGridViewComboBoxColumn.ValueMember 设置为另一列

Posted

技术标签:

【中文标题】将 DataGridViewComboBoxColumn.ValueMember 设置为另一列【英文标题】:Set DataGridViewComboBoxColumn.ValueMember to an other column 【发布时间】:2016-08-04 08:11:15 【问题描述】:

我有一个带有 DataSources 的 DataGridView。一个 ComboBoxColumn 一个 DataSource,整个 DataGridView 一个 DataSource。

如何根据使用“主”数据源设置的 ID 为我的 DataGridViewComboBoxColumn 显示正确的文本?

this.dgvFeinfilter.AutoGenerateColumns = false;
this.dgvFeinfilter.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;

DataGridViewTextBoxColumn colFFZNr = new DataGridViewTextBoxColumn();
colFFZNr.Name = this.ffznr;
colFFZNr.DataPropertyName = this.ffznr;
colFFZNr.Visible = false;

DataGridViewTextBoxColumn colFFFnr = new DataGridViewTextBoxColumn();
colFFFnr.Name = this.fffnr;
colFFFnr.DataPropertyName = this.fffnr;
colFFFnr.Visible = false;

DataGridViewTextBoxColumn colFFnr = new DataGridViewTextBoxColumn();
colFFnr.Name = this.ffnr;
colFFnr.DataPropertyName = this.ffnr;
colFFnr.Visible = false;

DataGridViewTextBoxColumn colFFPosition = new DataGridViewTextBoxColumn();
colFFPosition.Name = this.ffPosition;
colFFPosition.HeaderText = this.headerReihenfolge;
colFFPosition.DataPropertyName = this.ffPosition;
colFFPosition.Visible = true;

DataGridViewComboBoxColumn colFeinfilter = new DataGridViewComboBoxColumn();
colFeinfilter.DataSource = this.DsView.Tables["Feinfilter"];
colFeinfilter.DropDownStyle = ComboBoxStyle.DropDownList;
colFeinfilter.HeaderText = this.headerFeinfilter;
colFeinfilter.DataPropertyName = this.ffKennung; //this.ffnr;
colFeinfilter.DisplayMember = this.ffKennung;
colFeinfilter.ValueMember = this.ffnr;
colFeinfilter.Name = this.ffKennung;
colFeinfilter.Width = 300;

DataGridViewTextBoxColumn colDelete = new DataGridViewTextBoxColumn();
colDelete.Name = "MarkForDelete";
colDelete.Visible = false;
colDelete.DefaultCellStyle.NullValue = 0;

EnumerableRowCollection<DataRow> query = from zuordnung in this.DsWork.Tables["FFZuordnung"].AsEnumerable()
                                         where zuordnung.Field<Decimal>(this.fffnr) == this.feinfilterfolge.FFFNr
                                         select zuordnung;

DataView newView = query.AsDataView();
this.dgvFeinfilter.DataSource = newView;
this.dgvFeinfilter.Columns.AddRange(colFFPosition, colFeinfilter, colFFZNr, colFFFnr, colFFnr, colDelete);

数据源 1 (this.DsView.Tables["Feinfilter"]):

SELECT
    FFSPNR,    -- id
    FFKENNUNG  -- name
FROM
    Feinfilter
WHERE
    FFKENNUNG IS NOT NULL

数据源 2 (this.DsWork.Tables["FFZuordnung"]):

SELECT
    FFZLFDNR,        --id
    FFSPNR,          --fk
    FFFNr,           --fk
    FFZREIHENFOLGE,  --number
    TSUPDATE         --datetime
FROM
    FFZUORDNUNG

绑定所有(也是不可见的)Columns 后具有正确的值。除非DataGridViewComboBoxColumn。该值为空。如果我将 colFeinfilter.DataPropertyName = this.ffKennung; 更改为 colFeinfilter.DataPropertyName = this.ffnr;,则会显示正确的 ID。但我需要“ffKennung”。

【问题讨论】:

【参考方案1】:

你需要设置

colFeinfilter.DataPropertyName = this.ffnr;
colFeinfilter.DisplayMember = this.ffKennung;

DataPropertyName 的值将定义必须选择的值DisplayMember 的值将用于显示所选值的文本

【讨论】:

以上是关于将 DataGridViewComboBoxColumn.ValueMember 设置为另一列的主要内容,如果未能解决你的问题,请参考以下文章

如何将Ios文件上传到

Javascript 将正则表达式 \\n 替换为 \n,将 \\t 替换为 \t,将 \\r 替换为 \r 等等

如何将视频文件转换格式

sh 一个将生成CA的脚本,将CA导入到钥匙串中,然后它将创建一个证书并与CA签名,然后将其导入到

python怎么将0写入文件?

如何将CMD窗口背景改成透明?