i am trying to select a given number from my list box but when i run the code it returns null
i have tried.selectedvalue i have tried.selecteditem and.selecteditems all return null
ID = ExpireditemInfo.GetItemText(ExpireditemInfo.SelectedItem);
ID is a string
This is my source i use dapper
loadinventory = iteminfo.GetExpireinfo(now,expirationDate);
ExpirationID.DataSource = loadinventory;
ExpirationID.DisplayMember = "Expireing_ID";
my data model
public string Transaction_ID { get; set; }
public string Type_Of_Transaction { get; set; }
public string Name { get; set; }
public string Quantity { get; set; }
public string Unit { get; set; }
public string Expireing_ID
{
get
{
return $"{Transaction_ID}";
}
}
i want to select the number 6 from the list-box etc and store that selection in a string which i will then convert to an int to pass along through a stored procedure. the int is for the id column of a database so it has to be an in when passing to the database.
is it that the 6 is not being recognized as a string and if so how do i select it?
i am trying to select a given number from my list box but when i run the code it returns null
After seeing the way you are binding to your ListBox
, it seems you are not setting the ValueMember
property, doing so you can have an actual DisplayMember
and a ValueMember
that represents the actual underlying value of that display object.
Here's what I would do, this is just one example:
// Holds the actual selected items value
public string ExpirationIDValue {get; private set;}
// Anytime the selected index changes, we update our property. You can
// put this on the constructor and or on load.
ExpirationID.SelectedIndexChanged += ExpirationID_SelectedIndexChanged;
// Event handler for when selection changes
private void ExpirationID_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is ListBox listBox && listBox.SelectedValue != null)
{
ExpirationIDValue = listBox.SelectedValue.ToString();
}
}
// This was for testing, just to get some data, you can ignore this
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("Expireing_ID",typeof(string)));
dataTable.Columns.Add(new DataColumn("Expireing_ID_Display",typeof(string)));
int i = 1;
while(i < 10)
{
dataTable.Rows.Add(i.ToString(),$"Display {i}");
i++;
}
// Here you need to make sure you set these properties to your actual
// property names.
ExpirationID.DataSource = dataTable;
ExpirationID.DisplayMember = "Expireing_ID_Display";
ExpirationID.ValueMember = "Expireing_ID";
Note: your selectedindexchanged event may fire early and cause an exception, what you can do is create a variable bool and on loading set it true and then in the event make sure that it's not true. At the end of loading the data, set that back to false.
Hopefully this will fix your issue, if there's something you don't understand or need further clarification, please let me know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.