将DataGridViewComboBoxColumn.ValueMember设置为其他列

时间:2016-08-04 08:11:16

标签: c# datagridview

我有一个DataGridView与DataSources。一个ComboBoxColumn的一个DataSource和整个DataGridView的一个DataSource。

如何显示我的DataGridViewComboBoxColumn的正确文本取决于使用" main"设置的ID。数据源?

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

DataSource 1(this.DsView.Tables [&#34; Feinfilter&#34;]):

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

DataSource 2(this.DsWork.Tables [&#34; FFZuordnung&#34;]):

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

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

1 个答案:

答案 0 :(得分:0)

您需要设置

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

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