methuselah
methuselah

Reputation: 13216

Additional information: Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'

I'm having a weird issue with my code. I'm currently coding a filter for my datagrid.

Whenever the user clears the text field - the following error message is brought up:

Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

This is my code so far:

    private void driverNo_TextChanged(object sender, EventArgs e)
    {

        // if driverNo text is empty then return all rows

        if (string.IsNullOrEmpty(driverNo.Text))
        {
            ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = string.Empty;
            return;
        }

        // if driverNo is a numerical value then view result

        int temp;
        if (int.TryParse(driverNo.Text, out temp))
            ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "DriverNo = " + driverNo.Text;
        else
            MessageBox.Show("Invalid driver number.");
            driverNo.Text = "";
    }

Upvotes: 2

Views: 16464

Answers (3)

Leeroy
Leeroy

Reputation: 1

        `xxxBindingSource.AddNew();`
        `dataGridView1.CurrentRow.Cells[0].Value = "Value Here";`
        `dataGridView1.CurrentRow.Cells[1].Value = "Value here";`
        `xxxBindingSource.EndEdit();`

Upvotes: -1

RameezAli
RameezAli

Reputation: 956

DataTable dt = ((DataView)gridView1.DataSource).Table;

DataView dv = (DataView)gridView1.DataSource;

Upvotes: 1

Adam Houldsworth
Adam Houldsworth

Reputation: 64527

The value of DataSource is a type of BindingSource, not DataTable. Basically your expectation is incorrect. It is possible the DataTable is actually backing the BindingSource.

You very likely have a BindingSource component on your WinForms design surface. You can get to the table via something like the following:

var bindingSource = this.myBindingSource;
var dt = (DataTable)bindingSource.DataSource;

You can indirectly get to it via:

var bindingSource = (BindingSource)dataGridView1.DataSource;
var dt = (DataTable)bindingSource.DataSource;

For your code it might look like this:

private void driverNo_TextChanged(object sender, EventArgs e)
{

    // if driverNo text is empty then return all rows

    if (string.IsNullOrEmpty(driverNo.Text))
    {
        var bindingSource = (BindingSource)dataGridView1.DataSource.
        var table = (DataTable)bindingSource.DataSource;
        table.DefaultView.RowFilter = string.Empty;
        return;
    }

    // if driverNo is a numerical value then view result

    int temp;
    if (int.TryParse(driverNo.Text, out temp))
    {
        var bindingSource = (BindingSource)dataGridView1.DataSource.
        var table = (DataTable)bindingSource.DataSource;
        table.DefaultView.RowFilter = "DriverNo = " + driverNo.Text;
    }
    else
        MessageBox.Show("Invalid driver number.");
        driverNo.Text = "";
}

Upvotes: 3

Related Questions