473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listbox control - valuemember & selectedvalue

Pat
I'm a newbie to c# and could use some help - been banging my head on
the keyboard for 3 days now.

I have an unbound listbox that I'm populating this way:

1. loop through a datatable and load row into row object
2. Check one of the row columns against a dictionary key
3. If it's found, add it to the listbox.

Then I want to pass the actual item that shows up in the listbox
(empname) and the valuemember (empnum) to another form via properties
(I think). But I cannot get the value unless I bind the listbox to the
datatable - and then I get all the rows in the table, even if I set the
source "above" the loop. What I get is an error on the line where I
try to extract the selectedvalue.

public void LoadListBox()
{
dtEmp = taEmp.GetData() ;

for (int i = 0; i < dtEmp.Count; i++)
{
rEmp = dtEmp[i];
lbAssignedEmps. DisplayMember = rEmp.EMPNAME;
lbAssignedEmps. ValueMember = rEmp.EMPNUM;
// lbAssignedEmps. SelectedValue = rEmp.EMPNUM;

if (htPass.Contain sKey(rEmp.EMPNU M))
{
this.lbAssigned Emps.Items.Add( rEmp.EMPNAME);
}
}
}

then:

private void btnEdit_Click(o bject sender, EventArgs e)
{

string sEmpName = "";
string sEmpNumber = "";
string sPassword = "";

int iIndex = 0;
iIndex = lbAssignedEmps. SelectedIndex;
sEmpName = lbAssignedEmps. SelectedItem.To String();
sEmpNumber = lbAssignedEmps. SelectedValue.T oString(); ERROR
HERE!!
more stuff...
}

It says I must use the new keyword to create an instance of something -
I tried DataView, all sorts of things for 3 days.

I think a dictionary with a dictionary key (or value) would work, like
Dictionary<Dict ionary<string, string>, string>. I can declare it but
can't figure out how to populate it or extract it (syntax for the inner
dictionary). I need to write it to a .csv and then read it back into a
dictionary and again, use it to populate list boxes.

any help would be greatly appreciated!!! TIA
Pat

Jun 18 '06 #1
2 9800

Why don't you just create a new DataTable, fill it with new rows containing
only the ItemArray information that passes your "test", and then bind with
that?

Also, it's getting a bit late here, but I'm wondering if your first line
here:

sEmpName = lbAssignedEmps. SelectedItem.To String(); <<-----
sEmpNumber = lbAssignedEmps. SelectedValue.T oString(); ERROR

-shouldn't it be "SelectedItem.T ext" instead of SelectedItem? SelectedItem
is a ListItem, not sure if that would work.
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pat" wrote:
I'm a newbie to c# and could use some help - been banging my head on
the keyboard for 3 days now.

I have an unbound listbox that I'm populating this way:

1. loop through a datatable and load row into row object
2. Check one of the row columns against a dictionary key
3. If it's found, add it to the listbox.

Then I want to pass the actual item that shows up in the listbox
(empname) and the valuemember (empnum) to another form via properties
(I think). But I cannot get the value unless I bind the listbox to the
datatable - and then I get all the rows in the table, even if I set the
source "above" the loop. What I get is an error on the line where I
try to extract the selectedvalue.

public void LoadListBox()
{
dtEmp = taEmp.GetData() ;

for (int i = 0; i < dtEmp.Count; i++)
{
rEmp = dtEmp[i];
lbAssignedEmps. DisplayMember = rEmp.EMPNAME;
lbAssignedEmps. ValueMember = rEmp.EMPNUM;
// lbAssignedEmps. SelectedValue = rEmp.EMPNUM;

if (htPass.Contain sKey(rEmp.EMPNU M))
{
this.lbAssigned Emps.Items.Add( rEmp.EMPNAME);
}
}
}

then:

private void btnEdit_Click(o bject sender, EventArgs e)
{

string sEmpName = "";
string sEmpNumber = "";
string sPassword = "";

int iIndex = 0;
iIndex = lbAssignedEmps. SelectedIndex;
c> HERE!!
more stuff...
}

It says I must use the new keyword to create an instance of something -
I tried DataView, all sorts of things for 3 days.

I think a dictionary with a dictionary key (or value) would work, like
Dictionary<Dict ionary<string, string>, string>. I can declare it but
can't figure out how to populate it or extract it (syntax for the inner
dictionary). I need to write it to a .csv and then read it back into a
dictionary and again, use it to populate list boxes.

any help would be greatly appreciated!!! TIA
Pat

Jun 18 '06 #2
Pat
Thanks, Peter

After much fiddling, I finally got the whole dataset, datatable,
datarow thing working. It was much easier when loading a datatable
from the DB!! My main experience is backend database programming, and
though I've done some vb6 work on an existing product, starting from
scratch on c# front-end is fun, but challenging!

Thanks again,
Pat

Jun 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2024
by: Supa Hoopsa | last post by:
Working on a windows form, I am trying to bind a listbox to a dataset and want to set the displaymember & Valuemember properties of the listbox so that I can retrieve the selectedvalue from a selecteditem in the listbox. My code is very simple: lstReports.DataBindings.Clear() lstReports.DataSource = dsDataSet.Tables("Reports") lstReports.DisplayMember = "ReportDescription" lstReports.ValueMember = "ReportsKey"
2
3330
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's ValueMember is a ID that is a column in the table that the listbox uses. Is there a way to link these 2's relation so that when the user changes the selection in the Combox the list box would automacticall changes to reflect the items that's linked to...
6
12919
by: Alpha | last post by:
I have a listbox with datasource from a dataview. When a user selects a different item in a combobox then I need to refresh the listbox to the appropriate listing based on that combobox's selected value which is included in the listbox's filtering statement. Is the only way to do this is to dispose the dataview and then create a new one and then bind it to the listbox? Is there a better way than this? Thanks, Alpha
2
1968
by: Alpha | last post by:
I have a window application. On one of the form, there is a listbox and a few combox. The lstSchItem has a dataview as a datasource. The comboxes are bind to its selected value but through the dataset table. Now when I change the selection in the lstSchItem the comboxes are staying the same and not reflecting the changes. I tried changing the binding to the dataview.table but the comboxes are still not changing to reflect the...
0
2661
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem I am having is that when I have a value in the propertygird and edit that, I want the listbox to have the selectvalue equal to the value that is being edited. Just to make it clearer: PropgridVal = Germany Datasource=
1
2779
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and am having trouble getting it to do anything. The relevant code is below, followed by a better explanation of what I'm trying to do. // pseudocode start public delegate string StepMethod();
1
3232
by: fiaolle | last post by:
Hi I Can't get the MSDN's example for Listbox to work.The example is below. I get an error in Listbox's sub ListBox1_SelectedValueChanged at row textBox1.Text = ListBox1.SelectedValue and it happens after the line ListBox1.ValueMember = "ShortName" is run. The error says Cast from type 'USState' to type 'String' is not valid. I don't know why the code doesn't work, can anyone please explain that for me.
2
5544
by: =?Utf-8?B?U3RlcGhlbiBSaXRjaGll?= | last post by:
Hi NET1.1 / Winforms I have a listbox that I am binding to a table via the DataSource property. However I want to be able to programmatically select values in this listbox so I am using the code below... With lstJobTypes .DataSource = BusinessRules.Common.JobTypes.All(True)
2
3470
by: =?Utf-8?B?UGFvbG8=?= | last post by:
From a combo box I use the SelectedValue property as a parameter to a query from which I populate a ListBox private void cmbxCategory_SelectedValueChanged(object sender, EventArgs e) { string theCatId = cmbxCategory.SelectedValue.ToString(); var getSubCat = from sc in dataSet.SubCategory
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.