简体   繁体   中英

How to update a DataGridView in a Windows form when the end-user adds selection criteria

I have a data grid view that shows all rows of a particular database table on a Windows form. Above the grid, I have text fields with corresponding buttons that a user can use to narrow down the data grid view by various criteria, such as By Customer Number, By Order Number, etc... I haven't been able to figure out how to accomplish this.

I've tried many examples I've found online, the most recent resulting the code below.

OleDbConnection connection = SerialsDatabaseDB.GetConnection();
      string selectStatement
          = "Select * "
          + "FROM Orders "
          + "WHERE CustomerNumber = @CustomerNumber";
      OleDbCommand selectCommand =
          new OleDbCommand(selectStatement, connection);
      selectCommand.Parameters.AddWithValue("@CustomerNumber", custNumber);

      OleDbDataAdapter dataAdapter = 
          new OleDbDataAdapter(selectStatement, connection);
      DataSet dataSet = new DataSet("OrdersByCustomer");
      connection.Open();
      dataAdapter.Fill(dataSet, "OrdersByCustomer");
      connection.Close();
      dataGridView1.DataSource = dataSet;
      dataGridView1.DataMember = "OrdersByCustomer";

Currently, I'm getting a "No value given for one or more required parameters" on the dataAdapter.Fill(dataSet, "OrdersByCustomer"); line.

Try your code with this change

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(selectCommand);

Ole​DbData​Adapter has 4 constructors. One of them takes an instance of OleDbCommand . In your code you are creating it but you are not passing it into relevant constructor. The parameter you created and added to the selectCommand is just remaining unused. The exception you are getting is the result of this.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM