1

我想使用两个不同的文本框来过滤数据库中的信息以显示棒球运动员的平均得分。选择 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;
        }
4

2 回答 2

0

RowFilter不支持BETWEEN运营商。它也不支持像您这样的完整 sql 查询。但是,您可以在此处指定WHERE-clause 来过滤视图的行:

view.RowFilter = String.Format(CultureInfo.InvariantCulture.NumberFormat,
                 "BattingAverage >= {0} AND BattingAverage <= {1}", minimum, maximum);

我已经用于InvariantCulture.NumberFormat确保String.Format所需的英文格式(点作为小数分隔符)RowFilter

请注意,您还使用撇号包装了这些值,这在数字上是不允许的,并且您将textBox2.Text其用作最小值最大值的输入。

于 2013-01-16T09:02:06.550 回答
0

它现在通过添加 CultureInfo 来工作。// 搜索播放器,工作 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();
    }

//现在使用 CultureInfo private void button2_Click(object sender, EventArgs e) { decimal minimum = Convert.ToDecimal(textBox2.Text); 十进制最大值 = Convert.ToDecimal(textBox3.Text); // 应该显示不同分数的玩家 view.RowFilter = String.Format(CultureInfo.InvariantCulture.NumberFormat ,("BattingAverage >= {0} AND BattingAverage <= {1}" , minimum, maximum);

        if (textBox2.Text == "") view.RowFilter = string.Empty;
        if (textBox3.Text == "") view.RowFilter = string.Empty;
    }
于 2013-01-17T08:47:38.193 回答