/// <summary> /// Retrieves all rows in the DataGridView with a radio selection /// </summary> /// <param name="pDataGridView"></param> /// <param name="pBindingSource"></param> /// <param name="pIdentifierColumnName">Primary key</param> /// <param name="pFieldName">Field name displayed</param> /// <returns></returns> public static List <SelectedRadioButton> GetSelections(this DataGridView pDataGridView, BindingSource pBindingSource, string pIdentifierColumnName, string pFieldName) { var selectedRadioButtonList = new List <SelectedRadioButton>(); DataGridViewColumn col; foreach (DataGridViewRow row in pDataGridView.Rows) { var imageCell = row.Cells.OfType <DataGridViewImageCell>().FirstOrDefault(data => (int)data.Tag == (int)ImageSelection.Selected); if (imageCell == null) { continue; } col = pDataGridView.CurrentRow.Cells[imageCell.ColumnIndex].OwningColumn; selectedRadioButtonList.Add(new SelectedRadioButton() { RowIndex = imageCell.RowIndex, ColumnName = col.Name, ColumnIndex = col.Index, Rating = col.Index.ToEnum <Ratings>(), id = pBindingSource.DataTable().Rows[imageCell.RowIndex].Field <int>(pIdentifierColumnName), CompanyName = pBindingSource.DataTable().Rows[imageCell.RowIndex].Field <string>(pFieldName) }); } return(selectedRadioButtonList); }
private void toolStripButtonSave_Click(object sender, EventArgs e) { int errorCount = 0; // clear errors membersGrid.Rows.Cast <DataGridViewRow>().ToList().ForEach(row => row.ErrorText = ""); var originalTable = _bindingSource.DataTable(); var results = originalTable.AllChanges(); if (results.HasDeleted) { _operations.RemoveRows(results.DeletedPrimaryKeys); } if (results.HasNew) { if (results.HasNew) { foreach (DataRow row in results.Added.Rows) { var test = row.IsValid(); if (test.InValid) { _bindingSource.Position = _bindingSource.Find("id", test.Id); membersGrid.CurrentRow.ErrorText = test.Description; errorCount += 1; } } if (errorCount == 0) { _operations.AddRows(results.Added); } } } if (results.HasModified) { foreach (DataRow row in results.Modified.Rows) { var test = row.IsValid(); if (test.InValid) { _bindingSource.Position = _bindingSource.Find("id", test.Id); membersGrid.CurrentRow.ErrorText = test.Description; errorCount += 1; } } if (errorCount == 0) { _operations.UpdateRows(results.Modified); } } }
public static DataView RowFilterNewView(this BindingSource pSender, string pField, string pValue, bool pCaseSensitive = false) { pSender.DataTable().CaseSensitive = pCaseSensitive; pSender.DataView().RowFilter = $"{pField} = '{pValue.EscapeApostrophe()}'"; DataView view = new DataView(pSender.DataTable()) { RowFilter = $"{pField} = '{pValue.EscapeApostrophe()}'" }; return(view); }
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { var Cust = new Customer(); var f = new EditorForm(ref Cust); try { if (f.ShowDialog() == DialogResult.OK) { var ops = new Operations(); if (ops.AddCustomer(ref Cust)) { bsCustomer.DataTable().Rows.Add(new object[] { Cust.id, Cust.FirstName, Cust.LastName, Cust.CityIdentifier, Cust.StateIdentifer, Cust.City, Cust.State }); } else { MessageBox.Show(ops.ExceptionMessage); } } } finally { f.Dispose(); } }
/// <summary> /// Build select list from unbound DataGridViewCheckBox column /// </summary> /// <param name="sender"></param> /// <param name="ea"></param> private void cmdProcess_Click(object sender, EventArgs ea) { // ReSharper disable once TooWideLocalVariableScope List <int> identityList; try { // get checked rows which are used to create a DELETE FROM statement. identityList = (_bs.DataTable() .AsEnumerable() .Where(T => T.Field <bool>("Process")) .Select(T => T.Field <int>(_identityColumnName))) .ToList(); // Determine if there are one or more rows to create the DELETE statement if (identityList.Count > 0) { /* * No formal parameters needed e.g. cmd.Parameters.Add . . . */ var deleteStatement = $"DELETE FROM {_tableName} " + $"WHERE {_identityColumnName} IN " + $"({string.Join(",", identityList.ToArray().ToArray())})"; _deleteStatement = deleteStatement; /* * For learning */ if (Environment.UserName == "Karens") { Console.WriteLine(deleteStatement); } else { MessageBox.Show(deleteStatement); } } } catch (Exception e) { MessageBox.Show($"Selection or rows failed\n{e.Message}"); } }
/// <summary> /// This extension is a free form method for filtering. The usage would be /// to provide a user interface to put together the condition. See unit /// test FreeForm_CaseSensitive_OnBoth_Conditions_LastField_NotExact /// </summary> /// <param name="sender">BindingSource with a DataTable set for the DataSource</param> /// <param name="pFilter">Filter condition</param> /// <param name="pCaseSensitive">True for case sensitive, false for case insensitive</param> public static void RowFilterFreeForm(this BindingSource sender, string pFilter, bool pCaseSensitive = false) { sender.DataTable().CaseSensitive = pCaseSensitive; sender.DataView().RowFilter = pFilter; }
/// <summary> /// Apply a filter for ends with /// </summary> /// <param name="sender">BindingSource with a DataTable set for the DataSource</param> /// <param name="pField">Field to apply filter on</param> /// <param name="pValue">Value for filter</param> /// <param name="pCaseSensitive">True for case sensitive, false for case insensitive</param> public static void RowFilterEndsWith(this BindingSource sender, string pField, string pValue, bool pCaseSensitive = false) { sender.DataTable().CaseSensitive = pCaseSensitive; sender.DataView().RowFilter = $"{pField} LIKE '%{pValue}'"; }
/// <summary> /// Apply filter to the DefaultView of the DataTable /// </summary> /// <param name="sender">BindingSource with a DataTable set for the DataSource</param> /// <param name="pField">Field to apply filter on</param> /// <param name="pValue">Value for filter</param> /// <param name="pCaseSensitive">Filter should be case or case in-sensitive</param> /// <returns>Filter has rows</returns> public static bool RowFilter(this BindingSource sender, string pField, string pValue, bool pCaseSensitive = false) { sender.DataTable().CaseSensitive = pCaseSensitive; sender.DataView().RowFilter = $"{pField} = '{pValue}'"; return(sender.Count > 0); }
public static DataView DataView(this BindingSource sender) { return(sender.DataTable().DefaultView); }
/// <summary> /// Add new DataRow to DataTable where Identifier is a valid existing primary key /// </summary> /// <param name="sender"></param> /// <param name="Identifier">Existing primary key</param> /// <param name="CompanyName">Company name value</param> /// <param name="ContactName">Contact name value</param> /// <param name="ContactTitle">Contact title value</param> /// <param name="ContactTypeIdentifier">Corresponding identifier for contact title</param> public static void AddRow(this BindingSource sender, int Identifier, string CompanyName, string ContactName, string ContactTitle, int ContactTypeIdentifier) { sender.DataTable().Rows.Add(new object[] { Identifier, CompanyName, ContactName, ContactTypeIdentifier, ContactTitle }); }
/// <summary> /// Get last primary key regardless of the sort. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lastPrimaryKeyButton_Click(object sender, EventArgs e) { MessageBox.Show($"Last Key: {_bindingSource.DataTable().FieldLastValue<int>("CustomerIdentifier")}"); }
/// <summary> /// Apply a filter for Like containts /// </summary> /// <param name="pSender"></param> /// <param name="pField">Field to apply filter on</param> /// <param name="pValue">Value for filter</param> /// <param name="pCaseSensitive">Filter should be case or case in-sensitive</param> public static void RowFilterContains(this BindingSource pSender, string pField, string pValue, bool pCaseSensitive = false) { pSender.DataTable().CaseSensitive = pCaseSensitive; pSender.DataView().RowFilter = $"{pField} LIKE '%{pValue.EscapeApostrophe()}%'"; }
public static void RowFilterTwoConditions(this BindingSource pSender, string pField1, string pValue1, string pField2, string pValue2, bool pCaseSensitive = false) { pSender.DataTable().CaseSensitive = pCaseSensitive; pSender.DataView().RowFilter = $"{pField1} = '{pValue1.EscapeApostrophe()}' AND {pField2} = '{pValue2.EscapeApostrophe()}'"; }
/// <summary> /// Apply a filter for Like starts with /// </summary> /// <param name="sender"></param> /// <param name="field">Field to apply filter on</param> /// <param name="value">Value for filter</param> /// <param name="caseSensitive">Filter should be case or case in-sensitive</param> public static void RowFilterStartsWith(this BindingSource sender, string field, string value, bool caseSensitive = false) { sender.DataTable().CaseSensitive = caseSensitive; sender.DataView().RowFilter = $"{field} LIKE '{value.EscapeApostrophe()}%'"; }