FindNext on BindingSource
-
Anyone know a good method for implementing a findnext-ish function on a
BindingSource
?bindingSourceObject.Find
works great for finding single rows but won't find the next value. I'm thinking maybe declaring aDataRow
array and assigning its value withdataTableObject.Select
. Once I do that I usebindingSourceObject.IndexOf(dataRowArray[nextindex])
to get the desired position of the next row. Does anyone have any better, more efficient ideas? If so, I'd like to hear them. -
Anyone know a good method for implementing a findnext-ish function on a
BindingSource
?bindingSourceObject.Find
works great for finding single rows but won't find the next value. I'm thinking maybe declaring aDataRow
array and assigning its value withdataTableObject.Select
. Once I do that I usebindingSourceObject.IndexOf(dataRowArray[nextindex])
to get the desired position of the next row. Does anyone have any better, more efficient ideas? If so, I'd like to hear them.I guess I was on the right track. Here's the code that makes it work.
BindingSource _mainBS; string _fieldname; TARData.TARDataSet.DataRow[] _mRA; string _searchstring; int _currentPosition;
_mainBS
is assigned by a constructor, likewise_fieldname
.public void FindNext() { if (_searchstring != textBox1.Text) { _searchstring = textBox1.Text; _currentPosition = 0; string searchCrit = getColumnSearchString(((DataRowView)_mainBS.Current).DataView.Table.Columns[_fieldname].DataType); _mRA = (TARData.TARDataSet.Carter_TAR_MainRow[])((DataRowView)_mainBS.Current).DataView.Table.Select( searchCrit + " LIKE '%" + _searchstring + "%'"); } if (_mRA.Length > 0) { this._mainBS.Position = this._mainBS.Find("Main_ID", _mRA[_currentPosition]["Main_ID"]); ; } _currentPosition = (_currentPosition + 1) % _mRA.Length; }
Just in case_fieldname
is anint
type, I throw in this little tidbit to perform a convert.private string getColumnSearchString(Type type) { if (type == typeof(int)) return "Convert(" + _fieldname + ",System.String)"; else return _fieldname; }
Not too efficient, but I still like admiring it. Not a bad way to spend an hour of my day.