示例#1
0
        private void bindComboBox(ComboBox comboBox, BindingSource bindingSource)
        {
            comboBox.DataBindings.Clear();
            comboBox.Enabled = false;
            if (comboBox != null &&
                comboBox.Tag != null &&
                comboBox.Tag is string &&
                bindingSource != null &&
                bindingSource.DataSource is DataTable &&
                ((DataTable)bindingSource.DataSource).Columns.Contains(comboBox.Tag.ToString().Trim().ToLower()))
            {
                if (_sharedUtils != null)
                {
                    DataColumn dc = ((DataTable)bindingSource.DataSource).Columns[comboBox.Tag.ToString().Trim().ToLower()];
                    _sharedUtils.BindComboboxToCodeValue(comboBox, dc);
                    if (comboBox.DataSource.GetType() == typeof(DataTable))
                    {
                        // Calculate the maximum width needed for displaying the dropdown items and set the combobox property...
                        int maxWidth = comboBox.DropDownWidth;
                        foreach (DataRow dr in ((DataTable)comboBox.DataSource).Rows)
                        {
                            if (TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width > maxWidth)
                            {
                                maxWidth = TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width;
                            }
                        }
                        comboBox.DropDownWidth = maxWidth;
                    }
                }

                if (_performLookups)
                {
                    // Bind the SelectedValue property to the binding source...
                    comboBox.DataBindings.Add("SelectedValue", bindingSource, comboBox.Tag.ToString().Trim().ToLower(), true, DataSourceUpdateMode.OnPropertyChanged);
                    // Wire up to an event handler if this column is a date_code (format) field...
                    if (comboBox.Tag.ToString().Trim().ToLower().EndsWith("_code") &&
                        ((DataTable)bindingSource.DataSource).Columns.Contains(comboBox.Tag.ToString().Trim().ToLower().Replace("_code", "")))
                    {
                        comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                        comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                        comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
                    }
                }
                else
                {
                    // Bind the Text property to the binding source...
                    comboBox.DataBindings.Add("Text", bindingSource, comboBox.Tag.ToString().Trim().ToLower(), true, DataSourceUpdateMode.OnPropertyChanged);
                }

                // Bind to an event handler for formatting the background color...
                comboBox.TextChanged += new EventHandler(control_TextChanged);
            }
        }
示例#2
0
        private void bindComboBox(ComboBox comboBox, BindingSource bindingSource)
        {
            comboBox.DataBindings.Clear();
            comboBox.Enabled = false;
            if (comboBox != null &&
                comboBox.Tag != null &&
                comboBox.Tag is string &&
                bindingSource != null &&
                bindingSource.DataSource is DataTable &&
                ((DataTable)bindingSource.DataSource).Columns.Contains(comboBox.Tag.ToString().Trim().ToLower()))
            {
                if (_sharedUtils != null)
                {
                    DataColumn dc = ((DataTable)bindingSource.DataSource).Columns[comboBox.Tag.ToString().Trim().ToLower()];
                    _sharedUtils.BindComboboxToCodeValue(comboBox, dc);
                    if (comboBox.DataSource.GetType() == typeof(DataTable))
                    {
                        // Calculate the maximum width needed for displaying the dropdown items and set the combobox property...
                        int maxWidth = comboBox.DropDownWidth;
                        foreach (DataRow dr in ((DataTable)comboBox.DataSource).Rows)
                        {
                            if (TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width > maxWidth)
                            {
                                maxWidth = TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width;
                            }
                        }
                        comboBox.DropDownWidth = maxWidth;
                    }
                }


//                if (_sharedUtils != null &&
//                    _sharedUtils.LookupTablesContains("MRU_code_value_lookup"))
//                {
//                    DataColumn dc = ((DataTable)bindingSource.DataSource).Columns[comboBox.Tag.ToString().Trim().ToLower()];
////DataTable cvl = _lookupTables["MRU_code_value_lookup"];
//                    DataTable cvl = _sharedUtils.LookupTablesGetMRUTable("MRU_code_value_lookup");

//                    //if (dc.ExtendedProperties.Contains("code_group_id") && cvl != null)
//                    if (dc.ExtendedProperties.Contains("group_name") && cvl != null)
//                    {
//                        //DataView dv = new DataView(cvl, "code_group_id='" + dc.ExtendedProperties["code_group_id"].ToString() + "'", "display_member ASC", DataViewRowState.CurrentRows);
//                        DataView dv = new DataView(cvl, "group_name='" + dc.ExtendedProperties["group_name"].ToString() + "'", "display_member ASC", DataViewRowState.CurrentRows);
//                        DataTable dt = dv.ToTable();
//                        if (dc.ExtendedProperties.Contains("is_nullable") && dc.ExtendedProperties["is_nullable"].ToString() == "Y")
//                        {
//                            DataRow dr = dt.NewRow();
//                            foreach (DataColumn cvldc in cvl.Columns)
//                            {
//                                // If there are any non-nullable fields - set them now...
//                                if (!cvldc.AllowDBNull)
//                                {
//                                    dr[cvldc.ColumnName] = -1;
//                                }
//                            }
//                            dr["display_member"] = "[Null]";
//                            dr["value_member"] = DBNull.Value;
//                            dt.Rows.InsertAt(dr, 0);
//                            dt.AcceptChanges();
//                        }
//                        comboBox.DisplayMember = "display_member";
//                        comboBox.ValueMember = "value_member";
//                        comboBox.DataSource = dt;
//                    }

//                    // Calculate the maximum width needed for displaying the dropdown items and set the combobox property...
//                    int maxWidth = comboBox.DropDownWidth;
//                    foreach (DataRow dr in cvl.Rows)
//                    {
//                        if (TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width > maxWidth)
//                        {
//                            maxWidth = TextRenderer.MeasureText(dr["display_member"].ToString().Trim(), comboBox.Font).Width;
//                        }
//                    }
//                    comboBox.DropDownWidth = maxWidth;

//                }

                if (_performLookups)
                {
                    // Bind the SelectedValue property to the binding source...
                    comboBox.DataBindings.Add("SelectedValue", bindingSource, comboBox.Tag.ToString().Trim().ToLower(), true, DataSourceUpdateMode.OnPropertyChanged);
                    // Wire up to an event handler if this column is a date_code (format) field...
                    if (comboBox.Tag.ToString().Trim().ToLower().EndsWith("_code") &&
                        ((DataTable)bindingSource.DataSource).Columns.Contains(comboBox.Tag.ToString().Trim().ToLower().Replace("_code", "")))
                    {
                        comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                        comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                        comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
                    }
                }
                else
                {
                    // Bind the Text property to the binding source...
                    comboBox.DataBindings.Add("Text", bindingSource, comboBox.Tag.ToString().Trim().ToLower(), true, DataSourceUpdateMode.OnPropertyChanged);
                }

                // Bind to an event handler for formatting the background color...
                comboBox.TextChanged += new EventHandler(control_TextChanged);
            }
        }