I would like to use two different textboxes to filter information from a database to display baseball players average score. Example selecting 0,3 to 0,4, should display players with scores between these number.
Whats not working is the following code:
// Search for player, working
private void button1_Click(object sender, EventArgs e)
{
view.RowFilter = "LastName like '%" + textBox1.Text + "%'";
if (textBox1.Text == "") view.RowFilter = string.Empty;
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable datatable = new DataTable();
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mattias\Dropbox\C#\Database\Baseball.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
connection.Open();
datatable.Load(new SqlCommand("select * from players", connection).ExecuteReader());
dataGridView1.DataSource = view = datatable.DefaultView;
connection.Close();
}
// This button is not working
private void button2_Click(object sender, EventArgs e)
{
decimal minimum = Convert.ToDecimal(textBox2.Text);
decimal maximum = Convert.ToDecimal(textBox3.Text);
// Should display players with different scores
view.RowFilter = String.Format("BattingAverage >= {0} AND BattingAverage <= {1}"
, minimum, maximum);
if (textBox2.Text == "") view.RowFilter = string.Empty;
if (textBox3.Text == "") view.RowFilter = string.Empty;
}
RowFilterdoes not support theBETWEENoperator. It also doesn’t support complete sql queries like yours. However, you can specify theWHERE-clause there to filter the rows of the view:I’ve used
InvariantCulture.NumberFormatinString.Formatto ensure the english format(dots as decimal separator) inRowFilterwhich is required.Note that you also have wrapped the values with apostrophes which is not allowed on numbers and that you’re using
textBox2.Textas input for your minimum and maximum value.