我有一個部門表,我想在部門下選擇多個復選框,然后在資料網格中顯示它。當我選擇一個選項時,它可以作業,但是如何傳遞多個值?如 queryBuilder 影像所示WHERE userGroup=?。
在這里,我想根據復選框傳遞多個值:




uj5u.com熱心網友回復:
使用
表格代碼
using System;
using System.Linq;
using System.Windows.Forms;
using SimpleExamples.Classes;
using SimpleExamples.Extensions;
namespace SimpleExamples
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Shown = OnShown;
DataOperations.GetCommandText = ReceiveQuery;
}
/// <summary>
/// Show decoded SQL SELECT
/// </summary>
/// <param name="sender"></param>
private void ReceiveQuery(string sender)
{
label1.Text = sender;
}
private void OnShown(object sender, EventArgs e)
{
CompanyCheckedListBox.DataSource = DataOperations.GetCompanies();
}
private void GetSelectedButton_Click(object sender, EventArgs e)
{
CompanyListBox.DataSource = null;
var list = CompanyCheckedListBox.CheckedList();
if (list.Count <= 0) return;
var indices = list.Select(company => company.Id).ToList();
var (companies, exception) = DataOperations.GetByPrimaryKeys(indices);
if (exception is null)
{
CompanyListBox.DataSource = companies;
}
else
{
MessageBox.Show(exception.Message);
}
}
}
}
從 CheckedListBox 獲取檢查公司的語言擴展
public static class GenericExtensions
{
public static List<Company> CheckedList(this CheckedListBox source)
=> source.Items.Cast<Company>()
.Where((item, index) => source.GetItemChecked(index)).Select(item => item)
.ToList();
}
公司類
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => Name;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408841.html
標籤:
