数据行作为 DataGridView 的数据源

Datarow as DataSource of DataGridView

public partial class Oef455 : Form
{
    dtsOef444 dtsOrders = new dtsOef444();
    dtsOef444TableAdapters.tblOrderinformatieTableAdapter TAOrderInfo = new dtsOef444TableAdapters.tblOrderinformatieTableAdapter();
    dtsOef444TableAdapters.tblOrdersTableAdapter TAOrders = new dtsOef444TableAdapters.tblOrdersTableAdapter();
    public Oef455()
    {
        InitializeComponent();
    }

    private void cbOrderID_SelectedIndexChanged(object sender, EventArgs e)
    {
        dataGridView1.DataSource = null;
        int OrderID = int.Parse(cbOrderID.SelectedValue.ToString());
        DataRow drOrders = dtsOrders.tblOrders.FindByOrderID(OrderID);
        dataGridView1.DataSource = drOrders;
    }

    private void Oef455_Load(object sender, EventArgs e)
    {
        TAOrderInfo.Fill(dtsOrders.tblOrderinformatie);
        TAOrders.Fill(dtsOrders.tblOrders);
        cbOrderID.ValueMember = "OrderID";
        cbOrderID.DisplayMember = "OrderID";
        cbOrderID.DataSource = dtsOrders.tblOrders;
    }
}

我正在尝试在 DataGridView 中显示所有带有 selected OrderID(您可以在组合框中 select )的订单,有人可以帮助我吗?因为当我 select 组合框中的订单时,DataGridView 没有改变...

private void cbOrderID_SelectedIndexChanged(object sender, EventArgs e)
{
    dataGridView1.DataSource = null;
    int OrderID = int.Parse(cbOrderID.SelectedValue.ToString());
    DataRow drOrders = dtsOrders.tblOrders.FindByOrderID(OrderID);
    dataGridView1.DataSource = drOrders;
}

我假设您正在使用 TableAdapter 来填充 DataGridView
请尝试 .Fill 使用过滤后的 table。

private void cbOrderID_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbOrderID.SelectedValue != null){
        int OrderID = int.Parse(cbOrderID.SelectedValue.ToString());
        TAOrders.Fill(dtsOrders.tblOrders.FindByOrderID(OrderID));
    }
}

也许您还有其他 .Fill()此 TableAdapter 的功能。

另一种方法

尝试 .DefaultView.RowFilter 这将在不重新查询数据源的情况下进行过滤。 Whosebug - Filtering DataGridView without changing datasource

(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", textBoxFilter.Text);

这是一种可以根据 ComboBox 的选定项更改 DataGridView 的数据源的方法。

private void cbOrderID_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox combo = (sender as ComboBox);
    DataRowView selectedItem = (combo.SelectedItem as DataRowView);
    this.BindGrid(selectedItem);
}
private void BindGrid(DataRowView comboItem)
{
    int OrderID = (int)comboItem.Row["OrderID"];
    DataView view = new DataView(OrdersTable());
    view.RowFilter = string.Format("OrderID = {0}", OrderID);
    this.dataGridView1.DataSource = view;
}