private void ux_datagridview_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { DataGridView dgv = (DataGridView)sender; DataView dv = ((DataTable)((BindingSource)dgv.DataSource).DataSource).DefaultView; if (dv != null && e.ColumnIndex > -1) { DataColumn dc = dv.Table.Columns[e.ColumnIndex]; if (_sharedUtils.LookupTablesIsValidFKField(dc) && e.RowIndex < dv.Count && dv[e.RowIndex].Row.RowState != DataRowState.Deleted) { if (dv[e.RowIndex][e.ColumnIndex] != DBNull.Value) { e.Value = _sharedUtils.GetLookupDisplayMember(dc.ExtendedProperties["foreign_key_dataview_name"].ToString().Trim(), dv[e.RowIndex][e.ColumnIndex].ToString().Trim(), "", dv[e.RowIndex][e.ColumnIndex].ToString().Trim()); } dgv[e.ColumnIndex, e.RowIndex].ErrorText = dv[e.RowIndex].Row.GetColumnError(dc); e.FormattingApplied = true; } if (dc.ReadOnly) { e.CellStyle.BackColor = Color.LightGray; } if (dc.ExtendedProperties.Contains("is_nullable") && dc.ExtendedProperties["is_nullable"].ToString() == "N" && string.IsNullOrEmpty(dv[e.RowIndex][e.ColumnIndex].ToString())) { e.CellStyle.BackColor = Color.Plum; } } }
private void bindTextBox(TextBox textBox, BindingSource bindingSource) { textBox.DataBindings.Clear(); textBox.ReadOnly = true; if (textBox != null && textBox.Tag != null && textBox.Tag is string && bindingSource != null && bindingSource.DataSource is DataTable && ((DataTable)bindingSource.DataSource).Columns.Contains(textBox.Tag.ToString().Trim().ToLower())) { DataTable dt = (DataTable)bindingSource.DataSource; DataColumn dc = dt.Columns[textBox.Tag.ToString().Trim().ToLower()]; if (_performLookups && _sharedUtils.LookupTablesIsValidFKField(dc)) { // Create a new binding that handles display_member/value_member conversions... Binding textBinding = new Binding("Text", bindingSource, textBox.Tag.ToString().Trim().ToLower()); textBinding.Format += new ConvertEventHandler(textLUBinding_Format); textBinding.Parse += new ConvertEventHandler(textLUBinding_Parse); textBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged; // Bind it to the textbox... textBox.DataBindings.Add(textBinding); // Add an event handler for processing the first key press (to display the lookup picker dialog)... textBox.KeyDown += new KeyEventHandler(textBox_KeyDown); textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress); } else if (dc.DataType == typeof(DateTime)) { // Create a new binding that handles display_member/value_member conversions... Binding textBinding = new Binding("Text", bindingSource, textBox.Tag.ToString().Trim().ToLower()); textBinding.Format += new ConvertEventHandler(textDateTimeBinding_Format); textBinding.Parse += new ConvertEventHandler(textDateTimeBinding_Parse); textBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged; // Bind it to the textbox... textBox.DataBindings.Add(textBinding); } else { // Bind to the readonly datatable without lookups (because it is readonly and has already // had the Foreign Key and Code/Value lookup performed... textBox.DataBindings.Add("Text", bindingSource, textBox.Tag.ToString().Trim().ToLower()); } } // Bind to an event handler for formatting the background color... textBox.TextChanged += new EventHandler(control_TextChanged); }