| /** |
| * ./StateSchema/Schema.Designer.cs |
| * Класс-наследник от DataSet |
| * Генерируется на основании "Schema.xsd" в визуальном конструкторе схемы данных Visual Studio 2022 CE |
| */ |
| public partial class State : global::System.Data.DataSet { |
| //... |
| } |
|
| /** |
| * Попытка 1. Хотел реализовать его. |
| * Статический класс для создания одного экземпляра State на все приложение. |
| * Можно рассматривать как "DataModule" в Pascal |
| */ |
| public static class StaticStateAdapter |
| { |
| /** |
| * state наследник DataSet |
| * По идее, его можно использовать во всем проекте |
| */ |
| public static AppFileMonitoring.StateSchema.State state = new AppFileMonitoring.StateSchema.State(); |
| // ... статические методы для управления набором данных (загрузка, сохранение, конвертация) |
| } |
|
| /** |
| * Попытка 2. |
| * После неудачи с 1й попыткой, хотел отнаследоваться от постоянно перегенерируемого "State : DataSet". |
| * В дальнейшем была мысль "подсовывать" всем компонентам, которые используют этот класс, ссылки из статического экземпляра. |
| */ |
| public partial class StateAdapter : State |
| { |
| public static StateAdapter state; |
| static StateAdapter() { |
| state = new StateAdapter(); |
| } |
|
| /** |
| * Классический Singleton не проходит т.к. конструктор форм использует публичный конструктор. |
| * <code> |
| * private StateAdapter(){} |
| * </code> |
| */ |
| public StateAdapter() |
| { |
| //... |
| } |
|
| public static StateAdapter Instance { |
| get |
| { |
| return state; |
| } |
| } |
| public static StateAdapter GetInstance() |
| { |
| return state; |
| } |
|
| } |
| |
| // Вариант 1. |
| // Конструктор формы создает новый экземпляр State |
| /** |
| * FormMain.Designer.cs |
| */ |
| partial class FormMain |
| { |
| private System.Windows.Forms.DataGridView gridActivity; |
| private StateSchema.State state; |
| private System.Windows.Forms.BindingSource stateBindingSource; // Это добавил конструктор форм |
|
| private void InitializeComponent() |
| { |
| this.state = new AppFileMonitoring.StateSchema.State(); // Этого не надо. У нас уже есть глобальный экземпляр в StaticStateAdapter.state |
| ((System.ComponentModel.ISupportInitialize)(this.state)).BeginInit(); |
|
| this.state.DataSetName = "State"; |
| this.state.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; |
| this.stateBindingSource.DataSource = this.state; |
| this.stateBindingSource.Position = 0; |
|
| this.gridActivity = new System.Windows.Forms.DataGridView(); |
| ((System.ComponentModel.ISupportInitialize)(this.gridActivity)).BeginInit(); |
| this.gridActivity.AutoGenerateColumns = false; |
| this.gridActivity.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; |
| this.gridActivity.DataSource = this.stateBindingSource; // Вот этого не надо |
| this.gridActivity.Dock = System.Windows.Forms.DockStyle.Fill; |
| this.gridActivity.Location = new System.Drawing.Point(3, 59); |
| this.gridActivity.Name = "gridActivity"; |
| this.gridActivity.ReadOnly = true; |
| this.gridActivity.Size = new System.Drawing.Size(786, 315); |
| this.gridActivity.TabIndex = 2; |
| } |
|
|
| // Вариант 2. |
| // Происходит, если во вкладке "Источники данных" создать новый источник. |
| // 1. Выбираем "Источники данных" |
| // 2. Выбираем "Обїект". Жмем "Далее". |
| // 3. Выбираем в "Укажите объекты для привязки" AppFileMonitoring.StateAdapter |
| // Появляется источник данных "StateAdapter". |
| // Где он хранится? - ХЗ |
| // Когда создается? - ХЗ |
| // Потенциально то что нам надо. |
| // Конструктор формы создает новый экземпляр BindingSource, но экземпляр DataSource вроде как не создает. |
| /** |
| * FormMain.Designer.cs |
| */ |
| partial class FormMain |
| { |
|
| private System.Windows.Forms.BindingSource stateAdapterBindingSource; // Вот такой источник данных создал GUI мастер форм |
| private System.Windows.Forms.DataGridView gridDirs; // Вот к этому гриду надо привязать StaticStateAdapter.state |
|
| private void InitializeComponent() |
| { |
|
| this.stateAdapterBindingSource = new System.Windows.Forms.BindingSource(this.components); |
| ((System.ComponentModel.ISupportInitialize)(this.stateAdapterBindingSource)).BeginInit(); |
| // stateAdapterBindingSource |
| this.stateAdapterBindingSource.DataMember = "Dirs"; |
| this.stateAdapterBindingSource.DataSource = typeof(AppFileMonitoring.StateAdapter); // Вот интересный момент. Экземпляр не создается, но указывается нкжный тип данных для DataSource |
| this.stateAdapterBindingSource.CurrentChanged += new System.EventHandler(this.stateAdapterBindingSource_CurrentChanged); |
| ((System.ComponentModel.ISupportInitialize)(this.stateAdapterBindingSource)).EndInit(); |
|
|
| this.gridDirs = new System.Windows.Forms.DataGridView(); |
| ((System.ComponentModel.ISupportInitialize)(this.gridDirs)).BeginInit(); |
| this.gridDirs.AllowUserToOrderColumns = true; |
| this.gridDirs.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; |
| this.gridDirs.Dock = System.Windows.Forms.DockStyle.Fill; |
| this.gridDirs.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically; |
| this.gridDirs.Location = new System.Drawing.Point(3, 76); |
| this.gridDirs.Name = "gridDirs"; |
| this.gridDirs.ReadOnly = true; |
| this.gridDirs.Size = new System.Drawing.Size(786, 298); |
| this.gridDirs.TabIndex = 1; |
| } |