我正试图从带有C#的ListBox中获取所有选定的项目ValueMember.
例如:
我有这样一个列表:
ID | Name and Lastname
----------------------
1 | John Something
2 | Peter Something2
3 | Mary Smith
Run Code Online (Sandbox Code Playgroud)
这个结构是我的ListBox的一部分.我用这段代码构建了这个列表框:
private void fill_people_listBox()
{
this.listBoxPeople.DataSource = db.query("SELECT ....");
this.listBoxPeople.DisplayMember = "people_name_last";
this.listBoxPeople.ValueMember = "people_id";
}
Run Code Online (Sandbox Code Playgroud)
ListBox已成功填充.当用户想要保存更改时,我将遍历此列表以获取所有选定的项目,但我不需要该项目,我需要ID.
例:
1 | John Something.
Run Code Online (Sandbox Code Playgroud)
忽略John Something,得1,所以我不需要DisplayMember,只需要ValueMember.
为此,我尝试了以下几种方法:
1º
foreach (ListBox selectedItem in this.listBoxGarantes.SelectedItems)
{
selectedItem.ToString(); // just an example, I'm printing this.
}
Run Code Online (Sandbox Code Playgroud)
2º
string strItem;
// insert garantes
foreach (object selectedItem in this.listBoxGarantes.SelectedItems)
{
strItem = selectedItem as String;
strItem; …Run Code Online (Sandbox Code Playgroud) 好吧所以我有组合框,而数据源是linq查询的结果
//load QA names
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当我尝试添加下一行代码时
cboQASupervisor.ValueMember = "ID";
Run Code Online (Sandbox Code Playgroud)
我在运行时遇到错误,它无法转换匿名类型.我该如何解决?
更正:错误是:
无法绑定到新值成员.参数名称:value
以下代码更新填充了组合框cmbBox1.
OracleDataAdapter oraAdapter = new OracleDataAdapter(oraCmd);
DataSet oraDataSet = new DataSet();
oraAdapter.Fill(oraDataSet);
cmbBox1.ValueMember = oraDataSet.Tables[0].Columns["Val1"].ToString();
cmbBox1.DisplayMember = oraDataSet.Tables[0].Columns["Disp1"].ToString();
cmbBox1.DataSource = oraDataSet.Tables[0];
Run Code Online (Sandbox Code Playgroud)
我需要帮助来弄清楚如何从cmbBox1中删除几个值.我是否从ValueMember/DisplayMember中删除值,或者有什么方法可以隐藏cmbBox1中的值?请指教
如何编写这样的 C# winforms 代码?
CheckedListBox 项没有“Tag”和“ValueMember”属性。
我知道有很多替代方案。但我需要以这种方式工作。
private void LoadPermissionsToCheckedListBox()
{
Role selectedRole = (Role)comboBox1.SelectedItem;
int i = 0;
foreach (Permission p in selectedRole.PermissionItems)
{
checkedListBox1.Items.Add(p);
checkedListBox1.Items[i].Tag = p;
}
checkedListBox1.DisplayMember = "PermissionKey";
checkedListBox1.ValueMember = "PermissionID";
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建 ListBox,其中我将拥有键值对。我从课堂上获得的那些数据是从 getter 那里获得的。
班级:
public class myClass
{
private int key;
private string value;
public myClass() { }
public int GetKey()
{
return this.key;
}
public int GetValue()
{
return this.value;
}
}
Run Code Online (Sandbox Code Playgroud)
程序:
private List<myClass> myList;
public void Something()
{
myList = new myList<myClass>();
// code for fill myList
this.myListBox.DataSource = myList;
this.myListBox.DisplayMember = ??; // wanted something like myList.Items.GetValue()
this.myListBox.ValueMember = ??; // wanted something like myList.Items.GetKey()
this.myListBox.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
它类似于本主题 [ Cannot do key-value in listbox in …
valuemember ×5
c# ×4
listbox ×2
winforms ×2
.net ×1
combobox ×1
datasource ×1
function ×1
linq ×1
selecteditem ×1
tags ×1