如何设置datagridview中combox的默认显示值 datagridview 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 cell.Value 的值要与你绑定ValueMember值一致。 cell.Value 的值要与你绑定ValueMember值一致。cell.ValueMember绑定的事数据库的字段吗? cell.Value 的值要与你绑定ValueMember值一致。cell.ValueMember绑定的事数据库的字段吗?是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。 cell.Value 的值要与你绑定ValueMember值一致。cell.ValueMember绑定的事数据库的字段吗?是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。还是不行,显示没有那个字段执行DisplayMember=name 的时候说:没有这个字段。可是我数据库里明明就对应着这个字段 执行DisplayMember=namename需要替换成你自己的数据库列名啊怎么能直接粘进去用呢 this.ROLENAME.ValueMember = "Id"; this.ROLENAME.DisplayMember = "UserRole";我换成了我数据库里面的字段 你的datasource绑定的是个datatable吧,datatable列名没有修改过吧如果修改了,你需要绑定修改后的列名 设置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; } 建议 自己 绘制 combobox 控件 到 dgv 上因为 用 dgv 自带 的combobox 是绑定数据源的 很容易出错dgv添加combobox 应该分别设置ValueMember 和DisplayMember,cellvalue是DG的不是combox的。 如何通过按键来关闭另一窗口? listview使用的问题 有点难度的简单问题(按参数 创建 类名) DataGrid打开新的窗口。 C#编码问题! 用DataTable.NewRow()得到的行,为什么不能添加到相同结构的其它表中? 这个行在NewRow()方法内已经被表连系起来了吗? 【救命,在线等!!!】web程序打包问题 怎么处理网页返回数据 在程序中添加了excel引用,在生成主输出的时候出现错误。这是什么原因呀?? C# 中具体有那几项中心的技术呢? Visual Studio里如何生成一个类中的方法列表? C#实现获得wav文件播放总时间
cell.ValueMember绑定的事数据库的字段吗?
cell.ValueMember绑定的事数据库的字段吗?
是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。
cell.ValueMember绑定的事数据库的字段吗?
是的,比如 你读取数据库数据,读 id与name 设置ValueMember = id 值;DisplayMember=name 这个是显示的值。
还是不行,显示没有那个字段执行DisplayMember=name 的时候说:没有这个字段。可是我数据库里明明就对应着这个字段
name需要替换成你自己的数据库列名啊
怎么能直接粘进去用呢
this.ROLENAME.DisplayMember = "UserRole";我换成了我数据库里面的字段
datatable列名没有修改过吧
如果修改了,你需要绑定修改后的列名
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;
}
因为 用 dgv 自带 的combobox 是绑定数据源的 很容易出错
dgv添加combobox