- PR -

C# Grid内のComboBoxの背景色が勝手に変更される

投稿者投稿内容
イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:28
designerファイルC
コード:
            // tabPage2
            this.tabPage2.Controls.Add(this.m_dataGridView);
            this.tabPage2.Location = new System.Drawing.Point(4, 21);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(545, 196);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";
            this.tabPage2.UseVisualStyleBackColor = true;
            // m_dataGridView
            this.m_dataGridView.AllowUserToAddRows = false;
            this.m_dataGridView.AllowUserToDeleteRows = false;
            this.m_dataGridView.AutoGenerateColumns = false;
            this.m_dataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            this.m_dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.m_dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.MOVFCLNM,
            this.MOVFCLCD,
            this.PTN1,
            this.PTN2,
            this.PTN3,
            this.PTN4,
            this.PTN5,
            this.OGNDSPNO});
            this.m_dataGridView.DataSource = this.tBindingSource ;
            this.m_dataGridView.Location = new System.Drawing.Point(25, 18);
            this.m_dataGridView.Name = "m_dataGridView";
            this.m_dataGridView.RowHeadersVisible = false;
            this.m_dataGridView.RowTemplate.Height = 21;
            this.m_dataGridView.Size = new System.Drawing.Size(491, 159);
            this.m_dataGridView.TabIndex = 0;


イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:29
designerファイルD
コード:
            // mBindingSource
            this.mBindingSource.DataMember = "MMOVFCL";
            this.mBindingSource.DataSource = this.hDataSetBindingSource1;
            // hDataSetBindingSource1
            this.hDataSetBindingSource1.DataSource = this.hDataSet ;
            this.hDataSetBindingSource1.Position = 0;
            // hDataSet 
            this.hDataSet .DataSetName = "HDataSet ";
            this.hDataSet .SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // tBindingSource 
            this.tBindingSource .DataMember = "TOGNMOVFCL";
            this.tBindingSource .DataSource = this.hDataSet ;
            // tTableAdapter1 
            this.tTableAdapter1 .ClearBeforeFill = true;
            // mTableAdapter 1
            this.mTableAdapter 1.ClearBeforeFill = true;
            // tmTableAdapter1 
            this.tmTableAdapter1 .ClearBeforeFill = true;
            // mTableAdapter 
            this.mTableAdapter .ClearBeforeFill = true;
            // hDataSetBindingSource
            this.hDataSetBindingSource.DataSource = this.hDataSet ;
            this.hDataSetBindingSource.Position = 0;


イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:29
designerファイルE
コード:
            // MOVFCLNM
            this.MOVFCLNM.HeaderText = "NM";
            this.MOVFCLNM.Name = "MOVFCLNM";
            // MOVFCLCD
            this.MOVFCLCD.DataPropertyName = "MOVFCLCD";
            this.MOVFCLCD.DataSource = this.mBindingSource;
            this.MOVFCLCD.DisplayMember = "MOVFCLNM";
            this.MOVFCLCD.HeaderText = "CD";
            this.MOVFCLCD.Name = "MOVFCLCD";
            this.MOVFCLCD.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.MOVFCLCD.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.MOVFCLCD.ValueMember = "ITEMCODE";
            // PTN1
            this.PTN1.HeaderText = "1";
            this.PTN1.Name = "PTN1";
            // PTN2
            this.PTN2.HeaderText = "2";
            this.PTN2.Name = "PTN2";
            // PTN3
            this.PTN3.HeaderText = "3";
            this.PTN3.Name = "PTN3";
            // PTN4
            this.PTN4.HeaderText = "4";
            this.PTN4.Name = "PTN4";
            // PTN5
            this.PTN5.HeaderText = "5";
            this.PTN5.Name = "PTN5";
            // OGNDSPNO
            this.OGNDSPNO.DataPropertyName = "OGNDSPNO";
            this.OGNDSPNO.HeaderText = "OGNDSPNO";
            this.OGNDSPNO.Name = "OGNDSPNO";
            this.OGNDSPNO.Visible = false;


イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:31
designerファイルF
コード:
            // FormTest
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(592, 266);
            this.Controls.Add(this.tabControl1);
            this.Name = "FormTest";
            this.Text = "FormTest";
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.m_dataGridView)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.mBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.hDataSetBindingSource1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.hDataSet )).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.tBindingSource )).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.hDataSetBindingSource)).EndInit();
            this.ResumeLayout(false);
        }
        #endregion


イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:31
designerファイルG
コード:
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.Label label1;
        private HenseiAP.HDataSet TableAdapters.TTableAdapter tTableAdapter1 ;
        private System.Windows.Forms.DataGridView m_dataGridView;
        private HenseiAP.HDataSet TableAdapters.MTableAdapter  mTableAdapter 1;
        private HenseiAP.HDataSet TableAdapters.TMTableAdapter tmTableAdapter1 ;
        private System.Windows.Forms.BindingSource tBindingSource ;
        private System.Windows.Forms.BindingSource mBindingSource;
        private HenseiAP.HDataSet TableAdapters.MTableAdapter  mTableAdapter ;
        private System.Windows.Forms.BindingSource hDataSetBindingSource1;
        private HDataSet  hDataSet ;
        private System.Windows.Forms.BindingSource hDataSetBindingSource;
        private System.Windows.Forms.DataGridViewTextBoxColumn MOVFCLNM;
        private System.Windows.Forms.DataGridViewComboBoxColumn MOVFCLCD;
        private System.Windows.Forms.DataGridViewTextBoxColumn PTN1;
        private System.Windows.Forms.DataGridViewTextBoxColumn PTN2;
        private System.Windows.Forms.DataGridViewTextBoxColumn PTN3;
        private System.Windows.Forms.DataGridViewTextBoxColumn PTN4;
        private System.Windows.Forms.DataGridViewTextBoxColumn PTN5;
        private System.Windows.Forms.DataGridViewTextBoxColumn OGNDSPNO;
    }


イナバ
常連さん
会議室デビュー日: 2007/01/31
投稿数: 37
投稿日時: 2007-02-07 17:33
一部オブジェクト名を編集してありますが、以上です。
(このような内容でよろしいのでしょうか?質問の投げかけ方が悪いようでしたら、ご指摘ください。)

どうやらGridViewのAutoSizeRowModeの設定がNone以外の場合に背景が黒くなってしまうということがわかりました。
詳細につきましてはまだ引き続き調査しています。

お忙しい中、申し訳ございませんが、ご教授ください。
よろしくお願いいたします。
KI
大ベテラン
会議室デビュー日: 2007/01/10
投稿数: 239
投稿日時: 2007-02-09 15:22
本当にこれだけの量のコードがないと再現できないのでしょうか?
「最小限のコード」と私が言ったのは、記事を読んだ人が、
そのソースを貼り付けて事象を確認できるようにするためです。
再現するのにCellValidatingイベントとか本当に必要ですか?
タブコントロールに乗せないと発生しない事象なのですか?

現状がどうなっていて、何に困っているのかもいまいち理解できません。
そもそもコンボボックスは選択中のアイテムは反転表示されます。
そういうことではなくて…ですか?

背景色関係なら、一番あやしそうなのはDrawItemのハンドラです。
見たところ、SelectedIndexChangedは問題ないように見えます。
直接の解決になるかわかりませんが、気になった点を何点か挙げておきます。

MSDNのDrawItemの使用例にも載っていますが、通常はハンドラ内の記述は

e.DrawBackground()
文字列描画処理
e.DrawFocusRectangle()

になります。


気になったのは以下の3点です。

【1点目】
 DrawFocusRectangleを呼び出していませんが、意図的にそうしていますか?

【2点目】
 DrawBackgroundの前にreturnした場合はBackgroundが描画されませんが、意図的にそうしていますか?

【3点目】
 通常は文字列を描画する際のブラシはBlack固定ではなく、e.ForeColorの色のブラシを使います。
 e.ForeColor色のブラシを作成して書くと、選択中のアイテムの文字色は白になります。
 選択中のアイテムも黒で書いてしまうと見にくいと思いますが、こういう仕様でしょうか?
ベテラン
会議室デビュー日: 2005/05/16
投稿数: 85
お住まい・勤務地: 千葉県在住
投稿日時: 2007-02-09 17:05
梶です。
こちらも、以前のソースから必要な部分を抜き取って検証をしてみたのですが、現象確認には至りませんでした。

ちなみに、掲載して頂いたソースでは第3者環境でコンパイルできません。
(前提条件として必要なクラス等が存在しない為)

それと、私も気になった点が1つ。
EditingControlShowingイベントでDrawItem・SelectedIndexChangedイベントを設定してますが、毎回「+=」してしまっているので、先ず「-=」してから「+=」するべきかと・・・

それと、コントロールに対するアクセスを
コード:
((ComboBox)e.Control)


としてしまってますが、こんな感じ
コード:
ComboBox combo = e.Control as ComboBox;
if (combo != null)


で記述するのが良いかと思います。

スキルアップ/キャリアアップ(JOB@IT)