Autor Beitrag
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 27.01.09 18:16 
Hi,

für ein C#-Projekt suche ich eine Möglichkeit, mit den Standard-Komponenten vom VisualStudio eine Matrix-Ansicht ähnlich der TStringGrid von Delphi (mit FixedRows\FixedColumns=1) zu realisieren. Die Headerüberschriften hab ich auch inzwischen hinbekommen, nur die fixierte Spalte von der DataGridView sieht irgendwie noch wie ein schlechter AccessClon aus: Dort wöllte ich gern eigene Texte reinschreiben, find da nur nicht die richtigen Eigenschaften.

Hat da jemand mal für mich nen kurzen Hinweis, welche Eigenschaften ich mir mal gezielt anschauen sollte? Auch in Bezug auf VirtualMode wäre ich für ein paar kleine Hinweise dankbar.

MfG,
BenBE.

P.S.: Bisher sieht das bei mir (noch ohne meine interne Datenquelle) so hier aus:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(1212);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dataGridView1.Size = new System.Drawing.Size(240150);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.VirtualMode = true;
            this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
            this.dataGridView1.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.dataGridView1_CellValueNeeded);
            this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAltered);
            this.dataGridView1.ColumnAdded += new System.Windows.Forms.DataGridViewColumnEventHandler(this.dataGridView1_ColumnsAltered);
            this.dataGridView1.ColumnRemoved += new System.Windows.Forms.DataGridViewColumnEventHandler(this.dataGridView1_ColumnsAltered);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292273);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.ColumnCount = 4;
            dataGridView1.RowCount = 4;
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            System.Console.WriteLine(sender.ToString() + ": " + e.ToString());
        }

        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = e.ColumnIndex.ToString() + "," + e.RowIndex.ToString();
        }

        private void dataGridView1_ColumnsAltered(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.Index.ToString();
        }

        private void dataGridView1_RowsAltered(object sender, DataGridViewRowsAddedEventArgs e)
        {
            ((DataGridView)sender).Rows[e.RowIndex].Cells[0].Value = e.RowIndex.ToString();
        }
    }

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4808
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 28.01.09 10:34 
Schau dir mal die DataGridViewColumn.Frozen-Eigenschaft an (entspricht quasi FixedColumns). Und um die erste Spalte (sog. Spaltenheader) beim DGV zu entfernen, einfach DataGridView.RowHeadersVisible auf false setzen.

Und beim VirtualMode mußt du das CellValueNeeded-Ereignis behandeln (vorher muß aber die Größe (d.h. ColumnCount und RowCount) des DGV festgelegt worden sein und dadrin dann je Zelle 'e.Value' setzen.
BenBE Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 28.01.09 16:57 
Ich hab jetzt auch eine Lösung gefunden, die einfach die Zeilen-Header noch etwas beim Zeichnen erweitert. Die Vorlage dafür gibt's hier.

Meine Umsetzung ist dann so:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
        //--------------------------------------------------------------------------------------------------
        //Events for the DataGridView
        private void DGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int x = e.ColumnIndex;
            int y = e.RowIndex;

            //Inside the header
            if (y < 0)
            {
                return;
            }

            double v = (double)((DataGridView)sender).Rows[y].Cells[x].Value;

            if (x >= Matrix.width || y >= Matrix.height || x < 0 || y < 0)
            {
                System.Console.WriteLine("Change at " + x.ToString() + "," + y.ToString() + ": " + v.ToString());
                return;
            }
            else
            {
                Matrix.data[y, x] = v;
            }
            
            doMatrixChange();
        }

        private void DGV_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            int x = e.ColumnIndex;
            int y = e.RowIndex;

            if (x >= Matrix.width || y >= Matrix.height || x < 0 || y < 0)
            {
                e.Value = null;
            }
            else
            {
                e.Value = Matrix.data[y, x];
            }
        }

        private void DGV_ColumnsAltered(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.Index.ToString();
            e.Column.Width = 65;

            //            ((DataGridView)sender).Width = ((DataGridView)sender).RowHeadersWidth + ((DataGridView)sender).ColumnCount * ((DataGridView)sender).Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
        }

        private void DGV_RowsAltered(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //            ((DataGridView)sender).Height = ((DataGridView)sender).ColumnHeadersHeight + ((DataGridView)sender).RowCount * ((DataGridView)sender).RowTemplate.Height;
        }

        /// <summary>
        /// draw the row number on the header cell, auto adjust the column width
        /// to fit the row number
        /// </summary>
        /// <param name="e"></param>
        private void DGV_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;

            // get the row number in leading zero format, 
            //  where the width of the number = the width of the maximum number
            int RowNumWidth = dgv.RowCount.ToString().Length;
            String RowNumber = (e.RowIndex + 1).ToString();

            // get the size of the row number string
            SizeF Sz = e.Graphics.MeasureString(RowNumber, dgv.Font);

            // adjust the width of the column that contains the row header cells 
            if (dgv.RowHeadersWidth < (int)(Sz.Width + 20))
                dgv.RowHeadersWidth = (int)(Sz.Width + 20);

            // draw the row number
            e.Graphics.DrawString(
                RowNumber.ToString(),
                dgv.Font,
                SystemBrushes.ControlText,
                e.RowBounds.Location.X + dgv.RowHeadersWidth - 3 - Sz.Width,
                e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2));
        }


Die Settings für die DGV habsch das jetzt so gelöst:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(33);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dataGridView1.Size = new System.Drawing.Size(240150);
            this.dataGridView1.TabIndex = 1;
            this.dataGridView1.VirtualMode = true;
            this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.DGV_CellValueChanged);
            this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.DGV_RowPostPaint);
            this.dataGridView1.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.DGV_CellValueNeeded);
            this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.DGV_RowsAltered);
            this.dataGridView1.ColumnAdded += new System.Windows.Forms.DataGridViewColumnEventHandler(this.DGV_ColumnsAltered);
            this.dataGridView1.ColumnRemoved += new System.Windows.Forms.DataGridViewColumnEventHandler(this.DGV_ColumnsAltered);


Dass aber sowas allgemeines nicht per Default in einer DGV realisiert ist, wundert mich etwas ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.