Filter DataGridView based on multiple inputs without using Linq in WPF (by C#)

رضا جافری 1,291 Reputation points
2021-08-06T16:29:36.41+00:00

First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I want to be displayed similar cell values in DataGridView. So that these values are entered by the user.
Look at this photo:

121228-databasepng.png

There are 3 rows in the table and there are similar values in the first and third rows (last name = Smith and loan date = 20210608). I want to display them based on user input.

121179-filter.png

I got the information from the database with the following codes, but I can not display those rows in DataGridView.

        OleDbCommand OleDCmd = new OleDbCommand("Select * From MemberTable Where LastName='" +   
        LastName_TextBox.Text.Trim() + "'And LoanDate='" + LoanDate_TextBox.Text.Trim() + "'", OleDbConnect);  
        OleDCmd.CommandType = System.Data.CommandType.Text;  
        OleDbConnect.Open();  
        OleDbDataReader DataReader = OleDCmd.ExecuteReader();  
        int i = 0;  
        while (DataReader.Read())  
        {  
           //like this  
           //MemberDataGrid.RowStyle[i]=DataReader[all].ToString();  
            i++;  
        }  

Thanks

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
769 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-08-06T21:44:51.257+00:00

    For WPF use DataGrid instead of DataGridView. The following will fill a DataGrid from a SQL Server database.

    SqlConnection SqlConnect = new SqlConnection(connectionstring);  
    SqlConnect.Open();  
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM dbo.Courses", SqlConnect);  
    DataSet myDataSet = new DataSet();  
    adapter.Fill(myDataSet, "Courses");  
    Grid.DataContext = myDataSet;  
    Grid.ItemsSource = myDataSet.Tables[0].DefaultView;  
    

    You can use OleDbConnection instead of SqlConnection and OleDbDataAdapter instead of SqlDataAdapter.

    Filtering the data is much more complicated.

    0 comments No comments