private void textBoxSearch_TextChanged(object sender, EventArgs e) { DataTable data = (DataTable)dgv.DataSource; string filterField = dgv.Columns[1].Name; SearchTextChanging.Invoke(); data.DefaultView.RowFilter = string.Format(CultureInfo.InvariantCulture, "[{0}] LIKE '%{1}%'", filterField, textBoxSearch.Text); foreach (DataGridViewRow row in dgv.Rows) { RowData rowData = (RowData)row.Cells[2].Value; rowData.RowIndex = row.Index; } SearchTextChanged.Invoke(this, null); }
private void TextBoxSearch_TextChanged(object sender, EventArgs e) { DataTable data = (DataTable)dgv.DataSource; string filterField = dgv.Columns[1].Name; SearchTextChanging?.Invoke(); string searchString = textBoxSearch.Text.Trim(); string like = string.Empty; string[] splittedParts = searchString.Split(" "); if (splittedParts.Length > 1) { foreach (string splittedPart in splittedParts) { string and = string.Empty; if (!string.IsNullOrEmpty(like)) { and = $" AND [{filterField}]"; } like += $"{and} LIKE '%{splittedPart}%'"; } } else { like = $"LIKE '%{searchString}%'"; } try { data.DefaultView.RowFilter = $"[{filterField}] {like}"; } catch (Exception ex) { if (ex is EvaluateException || ex is SyntaxErrorException) { Log.Warn($"searchString \"{searchString}\" is a invalid", ex); } else { throw; } } if (string.IsNullOrEmpty(searchString)) { data.DefaultView.Sort = string.Empty; } else { string columnSortIndex = "SortIndex"; foreach (DataRow row in data.Rows) { if (row[1].ToString().StartsWith( searchString, StringComparison.InvariantCultureIgnoreCase)) { row[columnSortIndex] = 0; } else { row[columnSortIndex] = 1; } } data.DefaultView.Sort = columnSortIndex; } foreach (DataGridViewRow row in dgv.Rows) { RowData rowData = (RowData)row.Cells[2].Value; rowData.RowIndex = row.Index; } SearchTextChanged.Invoke(this, null); }