locked
Why is my datagridview starting at 0,0? RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace datagridtemp
    {
        public partial class Form1 : Form
        {
            private DataGridView PanelsBox = new DataGridView();
            Font DefaultSize = new Font("Microsoft Sans Serif", (float)10.75);
    
            public Form1()
            {
                InitializeComponent();
                
                this.Controls.Add(PanelsBox);
                PanelsBox.Name = "PanelsBox";
                PanelsBox.ColumnCount = 2;
                PanelsBox.Bounds = new Rectangle(new System.Drawing.Point(90, 108), new Size(300, 524));
                PanelsBox.Size = new Size(300, 524);
                PanelsBox.MinimumSize = new Size(300, 524);
                PanelsBox.MaximumSize = new Size(320, 524);
                PanelsBox.CellEndEdit += new DataGridViewCellEventHandler(PanelsBox_CellEndEdit);
                PanelsBox.CurrentCellChanged += new EventHandler(PanelsBox_CurrentCellChanged);
                PanelsBox.ColumnHeadersHeightChanged += new EventHandler(PanelsBox_ColumnHeadersHeightChanged);
                
    
                //PanelsBox.KeyDown += new KeyEventHandler(PanelsBox_KeyDown);
               // PanelsBox.KeyPress += new KeyPressEventHandler(PanelsBox_KeyPress);
    
                //good properties
                PanelsBox.RowHeadersVisible = false;
                PanelsBox.Dock = DockStyle.Fill;
                PanelsBox.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                PanelsBox.AllowUserToResizeRows = false;
                PanelsBox.AllowUserToResizeColumns = false;
                PanelsBox.AllowUserToDeleteRows = false;
                PanelsBox.ColumnHeadersHeight = 25;
                PanelsBox.MultiSelect = false;
                PanelsBox.BackgroundColor = Control.DefaultBackColor;
                PanelsBox.BorderStyle = BorderStyle.None;
    
                PanelsBox.Columns[0].Name = "PANEL ID";
                PanelsBox.Columns[0].Width = 70;
                PanelsBox.Columns[0].DefaultCellStyle.Font = DefaultSize;
                PanelsBox.Columns[0].ReadOnly = true;
                PanelsBox.Columns[1].Name = "PANEL NAME";
                PanelsBox.Columns[1].Width = 230;
                PanelsBox.Columns[1].DefaultCellStyle.Font = DefaultSize;
    
    
                //PanelsBox.Columns[0].HeaderCell.Style = ColumnHeaderStyle.NonClickable;
                /*DataGridViewImageColumn img = new DataGridViewImageColumn();
                Image image = Image.FromFile(@"C:\Users\Bane of the Sith\Desktop\120422\Green check.jpg");
                img.Image = image;
                PanelsBox.Columns.Add(img);
                img.HeaderText = "Image";
                img.Name = "img";*/
                //PanelsBox.Location = new System.Drawing.Point(90, 108);
                
               
                for (int i = 0; i < 100; i++)
                { PanelsBox.Rows.Add("2", ""); }
                PanelsBox.Rows.Add("276", "Main Lounge Gound Floor");
    
                /*PanelsBox.Columns[0].DisplayIndex = 3;
                PanelsBox.Columns[1].DisplayIndex = 4;
                PanelsBox.Columns[2].DisplayIndex = 0;
                PanelsBox.Columns[3].DisplayIndex = 1;
                PanelsBox.Columns[4].DisplayIndex = 2;*/
               
               
            }
    
            void PanelsBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                //throw new NotImplementedException();
                if (e.KeyChar == 13)
                { e.Handled = true; }
            }
    
            void PanelsBox_KeyDown(object sender, KeyEventArgs e)
            {
                //throw new NotImplementedException();
                if (e.KeyCode == Keys.Enter)
                { e.Handled = true;            }
            }
    
            
    
            void PanelsBox_CurrentCellChanged(object sender, EventArgs e)
            {
                //throw new NotImplementedException();
                System.Console.WriteLine(PanelsBox.CurrentRow.Index.ToString());
            }
    
            void PanelsBox_ColumnHeadersHeightChanged(object sender, EventArgs e)
            {
                //throw new NotImplementedException();
                if (PanelsBox.ColumnHeadersHeight != 25)
                PanelsBox.ColumnHeadersHeight = 25;
            }
    
            void PanelsBox_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                //throw new NotImplementedException();
                System.Console.WriteLine("ASD");
                System.Console.WriteLine(PanelsBox.CurrentCell);
                //System.Console.WriteLine(PanelsBox.CurrentCellAddress);
                int column = PanelsBox.CurrentCellAddress.X;
                int row = PanelsBox.CurrentCellAddress.Y;
                /*System.Console.WriteLine("X " + x);
                System.Console.WriteLine("Y " + y);*/
                System.Console.WriteLine(PanelsBox.Rows[row].Cells[column].Value);
                //PanelsBox.CurrentCell = PanelsBox[row-1, column];
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                // Clear the row error in case the user presses ESC.   
                
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
    
            }
        }
        class myDataGridView : DataGridView
        {
            protected override bool ProcessDialogKey(Keys keyData)
            {
                /*if (keyData == Keys.Enter)
                {
                    int col = this.CurrentCell.ColumnIndex;
                    int row = this.CurrentCell.RowIndex;
                    this.CurrentCell = this[col, row];
                    return true;
                }
                return base.ProcessDialogKey(keyData);*/
                if (keyData == Keys.Enter)
                    return base.ProcessDialogKey(Keys.Tab);
                else
                    return base.ProcessDialogKey(keyData);
            }
    
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                    int col = this.CurrentCell.ColumnIndex;
                    int row = this.CurrentCell.RowIndex;
                    this.CurrentCell = this[col, row];
                    e.Handled = true;
                }
                base.OnKeyDown(e);
            }
        } 
    }
    
    Ive tried a lot of combination, tried to do things, but it always starts at 0,0. Why?
    Monday, April 23, 2012 9:24 PM

Answers

  • Ive tried a lot of combination, tried to do things, but it always starts at 0,0. Why?

    Hi,

    do you mean the Location? If so, remove the Dock setting, because a DockStyle.Fill means also a location of 0,0

    Then set the Location like:

    PanelsBox.Dock = DockStyle.None;
    PanelsBox.Location = new Point(200, 200);

    Regards,

      Thorsten


    Monday, April 23, 2012 9:40 PM

All replies