CreateDataGridView

  protected virtual void OnDataGridView(object sender, EventArgs e)
        {
            DataGridView dataGridView = this.CreateDataGridView();
            if (dataGridView == null || dataGridView.Columns.Count == 0)
            {
                return;
            }
            dataGridView.AllowUserToDeleteRows = false;
            dataGridView.AllowUserToAddRows = false;
            dataGridView.ReadOnly = true;

            IVsWindowFrame frame = null;
            IVsUIShellOpenDocument shellOpenDoc = this.GetService<IVsUIShellOpenDocument>();
            string title = this.Title + "_table";
            if (VsShellUtilities.IsDocumentOpen(this, this.Title + "_table", out frame))
            {
                if (frame != null)
                {
                    frame.ActivateOwnerDockedWindow();
                }
            }
            else if (shellOpenDoc != null)
            {
                DataWindowPane pane = new DataWindowPane(this, new DataViewWindow(dataGridView));
                frame = shellOpenDoc.InitializeEditorInstance(__VSIEIFLAGS.IEI_CREATENEWDOCWIN_MASK, pane, null, title, Guid.Empty, null, Guid.Empty, title, title, null, 0, null, this, Guid.Empty);
                if (frame != null)
                {
                    frame.Show();
                }
            }
        }

        private DataGridView CreateDataGridView()
        {
            SharpDataGridView dataGridView = new SharpDataGridView();
            if (this.BindingSource != null && !this.BindingSource.IsBindingSuspended)
            {
                foreach (KeyValuePair<Control, FormExtender.ProvidedProperties> kvp in this.FormExtender.Controls)
                {
                    if (!string.IsNullOrEmpty(kvp.Value.DataPropertyName))
                    {
                        DataGridViewColumn column = null;
                        if (kvp.Key is CheckBox)
                        {
                            column = new DataGridViewCheckBoxColumn();
                        }
                        else if (kvp.Key is TextBoxBase)
                        {
                            TextBoxBase textBox = (TextBoxBase)kvp.Key;
                            if (kvp.Value.Required || kvp.Value.EditableOnlyNewRow)
                            {
                                DataGridViewTextBoxColumnEx col = new DataGridViewTextBoxColumnEx();
                                col.Required = kvp.Value.Required;
                                col.EditableOnlyNewRow = kvp.Value.EditableOnlyNewRow;
                                column = col; ;
                            }
                            else
                            {
                                column = new DataGridViewTextBoxColumn();
                            }
                            column.Width = textBox.Width;
                            column.ReadOnly = textBox.ReadOnly;

                        }
                        else if (kvp.Key is PropertyEditList)
                        {
                            PropertyEditList list = (PropertyEditList)kvp.Key;
                            DataGridViewRecordGroupColumn col = new DataGridViewRecordGroupColumn();
                            col.RecordGroup = list.RecordGroup;
                            col.ValueMember = list.ValueMember;
                            col.DisplayMember = list.DisplayMember;
                            col.EditorType = list.EditorType;
                            col.ConverterType = list.ConverterType;
                            col.Width = list.Width;
                            col.Required = kvp.Value.Required;
                            col.EditableOnlyNewRow = kvp.Value.EditableOnlyNewRow;
                            col.ReadOnly = list.ReadOnly;
                            foreach (ColumnMappingProperty property in list.ColumnMappingProperties)
                            {
                                col.ColumnMappingProperties.Add(property);
                            }
                            column = col;
                        }
                        else if (kvp.Key is PropertyEditControl)
                        {
                            PropertyEditControl edit = (PropertyEditControl)kvp.Key;
                            DataGridViewPropertyEditColumn col = new DataGridViewPropertyEditColumn();
                            col.Width = edit.Width;
                            col.Required = kvp.Value.Required;
                            col.EditableOnlyNewRow = kvp.Value.EditableOnlyNewRow;
                            col.ReadOnly = edit.ReadOnly;
                            column = col;
                        }
                        else if (!(kvp.Key is DataGridView))
                        {
                            column = new DataGridViewTextBoxColumn();
                        }
                        if (column != null)
                        {
                            if (column.ReadOnly)
                            {
                                column.DefaultCellStyle.BackColor = SystemColors.Control;
                            }
                            column.DataPropertyName = kvp.Value.DataPropertyName;
                            column.HeaderText = kvp.Value.DataPropertyName;
                            dataGridView.Columns.Add(column);
                        }
                    }
                }
                dataGridView.DataSource = this.BindingSource;
                foreach (DataGridViewColumn column in dataGridView.Columns)
                {
                    DataGridViewPropertyEditColumn propertyColumn = column as DataGridViewPropertyEditColumn;
                    if (propertyColumn != null)
                    {
                        PropertyDescriptor property = this.BindingSource.GetItemProperty(propertyColumn.DataPropertyName);
                        if (property != null)
                        {
                            propertyColumn.InitializeDataColumn(property);
                        }
                    }
                }
                return dataGridView;
            }
            else
            {
                return null;
            }
        }

posted @ 2010-06-20 17:04  leslie116  阅读(357)  评论(0编辑  收藏  举报