我在为Combobox设置SelectedIndex和SelectedValue时遇到问题。我想将组合框设置为从列表中选择的记录索引。
private void ReadEditedOperation()
{
using (var context = new DataClasses2DataContext())
{
var operation = context.operacje.FirstOrDefault(x => x.id_operacji == EditedId);
if (operation == null)
throw new ArgumentException("No operation with given id in the database");
OperationDto = new OperationDto()
{
Id = operation.id_operacji,
Data = operation.data,
Price = operation.kwota,
CategoryId = operation.id_kategoria,
UserId = operation.id_uzytkownika,
PaymentFormId = operation.id_forma_platnosci,
Description = operation.opis,
};
}
SetEditedOperationData();
}
private void SetEditedOperationData()
{
comboBoxUsers.SelectedIndex = OperationDto.UserId;
comboBoxCategory.SelectedItem = OperationDto.CategoryId;
//comboBoxSubcategory.SelectedValue = OperationDto.PodcategoryId;
dateTimePickerDate.Value = OperationDto.Data;
//radiobuttonexpense.checked = ;
comboBoxOperationForm.SelectedItem = OperationDto.PaymentFormId;
textBoxPrice.Text = OperationDto.Price.ToString("0.00");
richTextBoxDescription.Text = OperationDto.Description;
}
ComboBoxUsers.SelectedIndex有点用。当我选择ID为1的用户时,组合框将显示ID为2的用户。当我选择ID为2的用户时,它将显示用户ID为3的用户。当选择用户3时,它将失败。 ComboBoxUsers.SelectedValue根本不起作用。我不知道如何解决它。其余的,只有TextBoxes有效。
OperationDTO看起来像这样:
public class OperationDto
{
public int Id { get; set; }
public DateTime Data { get; set; }
public decimal Price { get; set; }
public int CategoryId { get; set; }
public string Category { get; set; }
public int PodcategoryId { get; set; }
public string Podcategory { get; set; }
public int UserId { get; set; }
public string Type { get; set; }
public int? PaymentFormId { get; set; }
public string PaymentForm { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string FullName => Name + " " + Surname;
}