Pregunta

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;
        }
¿Fue útil?

Solución

RowFilter does not support the BETWEEN operator. It also doesn't support complete sql queries like yours. However, you can specify the WHERE-clause there to filter the rows of the view:

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

I've used InvariantCulture.NumberFormat in String.Format to ensure the english format(dots as decimal separator) in RowFilter which is required.

Note that you also have wrapped the values with apostrophes which is not allowed on numbers and that you're using textBox2.Text as input for your minimum and maximum value.

Otros consejos

It now works by adding CultureInfo. // 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();
    }

// Now working with CultureInfo 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(CultureInfo.InvariantCulture.NumberFormat,("BattingAverage >= {0} AND BattingAverage <= {1}" , minimum, maximum);

        if (textBox2.Text == "") view.RowFilter = string.Empty;
        if (textBox3.Text == "") view.RowFilter = string.Empty;
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top