我想使用两个不同的文本框来过滤数据库中的信息以显示棒球运动员的平均得分。选择 0,3 到 0,4 的示例,应该显示得分在这些数字之间的球员。
什么不起作用是以下代码:
// 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;
}