I have a DataGridView which is bound to a datasource. I have also a TextBox to filter the records. In TextChanged event of the textbox I have a this line of code: i bind the gridview through designview of the form drag the gridview and choose datasource ... and then the member table.
(gvSideMember.DataSource as DataTable).DefaultView.RowFilter =
string.Format("F_NAME LIKE '%{0}%'", textSearch.Text);
But when I try to filter records it shows me that object reference is not set to an instance. The datasource has records. I don't know what's going on, kindly guide me, help will be appreciated.
textSearchnull?DataSourceis not aDataTable, then(DataSource as DataTable)returnsnull.askeyword if you are not prepared to accept that the result might benull! If you say(source as Table).Viewyou are not accepting that theasoperator might givenull. In that case you should use cast syntax,((Table)source).View, instead. When the former fails, you get an non-informative exception message Object reference not set to an instance. But with the latter, you get something like Unable to cast object of type 'INTERESTING_INFO' to type 'Table', and that helps you understand what you do. So in short: Avoidasin such cases.