Shagohad
Shagohad

Reputation: 398

Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'

This is my code:

   private void textBox1_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = string.Format("Produkt LIKE '%{0}%' Or Model LIKE '%{0}%' Or Firma LIKE '%{0}%'  ", textBox1.Text);
        dataGridView1.DataSource = dv;
        foreach (string s in dv)
        {
            list.Add(s);
        }
        if (textBox1.Text.Length == 0)
        {
            list.Clear();
        }
    }

There is no any error in program, so i can run it. But when i put some text into textbox, i'am receiving error like this: "Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'".

Could You tell me, what I am doing wrong ?.

Upvotes: 0

Views: 9107

Answers (2)

gzaxx
gzaxx

Reputation: 17600

You are trying to convert DataRowView to string here which is not possible and I assume that is not what you wanted.

foreach (DataRowView row in dv)
{
    list.Add(row["Model"].ToString()); //this will add models from selected rows to list
}

Upvotes: 3

Tobberoth
Tobberoth

Reputation: 9527

You can't do string s in dv since dv contains a collection of DataRowViews which are not strings. You need to get the string from the column/columns in the DataRowView instead.

Upvotes: 3

Related Questions