如何设置datagridview中combox的默认显示值

smileFiles 2014-09-18 04:32:47

例如:
foreach (DataGridViewRow row in dgvGroupList.Rows)
{

DataGridViewComboBoxCell cell = row.Cells[2] as DataGridViewComboBoxCell;
{

cell.Value ="aaa";

} }

如上,我遍历了这个datagridview,把每个combox的Value都赋值了,可是运行后:combox还是木有显示
值出来,求解
...全文
548 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
smthgdin_020 2014-09-19
  • 打赏
  • 举报
回复
应该分别设置ValueMember 和DisplayMember,cellvalue是DG的不是combox的。
_小黑_ 2014-09-19
  • 打赏
  • 举报
回复
建议 自己 绘制 combobox 控件 到 dgv 上 因为 用 dgv 自带 的combobox 是绑定数据源的 很容易出错 dgv添加combobox
xinxin_1741694057 2014-09-19
  • 打赏
  • 举报
回复
设置Combox控件的默认选项显示值 public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private void Frm_Main_Load(object sender, EventArgs e) { cbox_Select.SelectedIndex = 0;//默认选择第一项的值 } }//CodeGo.net/ partial class Frm_Main { // <summary> // 必需的设计器变量。 private System.ComponentModel.IContainer components = null; // 清理所有正在使用的资源。 // <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } // Windows 窗体设计器生成的代码 // 设计器支持所需的方法 - 不要 // 使用代码编辑器修改此方法的内容。 private void InitializeComponent() { this.cbox_Select = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); this.cbox_Select.FormattingEnabled = true; this.cbox_Select.Items.AddRange(new object[] { "第一项", "第二项", "第3项", "第4项", "第5项", "第6项"}); this.cbox_Select.Location = new System.Drawing.Point(170, 39); this.cbox_Select.Name = "cbox_Select"; this.cbox_Select.Size = new System.Drawing.Size(121, 20); this.cbox_Select.TabIndex = 0; this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(63, 42); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(101, 12); this.label1.TabIndex = 1; this.label1.Text = "默认选择第一项:"; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(355, 93); this.Controls.Add(this.label1); this.Controls.Add(this.cbox_Select); this.Name = "Frm_Main"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "设置ComboBox控件的默认选项为第一项"; this.Load += new System.EventHandler(this.Frm_Main_Load); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.ComboBox cbox_Select; private System.Windows.Forms.Label label1; }
於黾 2014-09-18
  • 打赏
  • 举报
回复
你的datasource绑定的是个datatable吧, datatable列名没有修改过吧 如果修改了,你需要绑定修改后的列名
smileFiles 2014-09-18
  • 打赏
  • 举报
回复
引用 8 楼 Z65443344 的回复:
执行DisplayMember=name name需要替换成你自己的数据库列名啊 怎么能直接粘进去用呢
this.ROLENAME.ValueMember = "Id"; this.ROLENAME.DisplayMember = "UserRole";我换成了我数据库里面的字段
於黾 2014-09-18
  • 打赏
  • 举报
回复
执行DisplayMember=name name需要替换成你自己的数据库列名啊 怎么能直接粘进去用呢
smileFiles 2014-09-18
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 baidu_20993989 的回复:] [quote=引用 4 楼 duanzi_peng 的回复:] [quote=引用 3 楼 baidu_20993989 的回复:] 没人知道吗?
cell.Value 的值要与你绑定ValueMember值一致。[/quote] cell.ValueMember绑定的事数据库的字段吗?[/quote] 是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。[/quote] 还是不行,显示没有那个字段执行DisplayMember=name 的时候说:没有这个字段。可是我数据库里明明就对应着这个字段
exception92 2014-09-18
  • 打赏
  • 举报
回复
引用 5 楼 baidu_20993989 的回复:
[quote=引用 4 楼 duanzi_peng 的回复:] [quote=引用 3 楼 baidu_20993989 的回复:] 没人知道吗?
cell.Value 的值要与你绑定ValueMember值一致。[/quote] cell.ValueMember绑定的事数据库的字段吗?[/quote] 是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。
smileFiles 2014-09-18
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
[quote=引用 3 楼 baidu_20993989 的回复:] 没人知道吗?
cell.Value 的值要与你绑定ValueMember值一致。[/quote] cell.ValueMember绑定的事数据库的字段吗?
exception92 2014-09-18
  • 打赏
  • 举报
回复
引用 3 楼 baidu_20993989 的回复:
没人知道吗?
cell.Value 的值要与你绑定ValueMember值一致。
smileFiles 2014-09-18
  • 打赏
  • 举报
回复
没人知道吗?
exception92 2014-09-18
  • 打赏
  • 举报
回复
设置它的ValueMember了没?
於黾 2014-09-18
  • 打赏
  • 举报
回复
试过直接设置cell.value吗

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧