将 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 设置为另一列的主要内容,如果未能解决你的问题,请参考以下文章
Javascript 将正则表达式 \\n 替换为 \n,将 \\t 替换为 \t,将 \\r 替换为 \r 等等