Results 1 to 5 of 5

Thread: c# Windows Application GridView Arrow Keys

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    Question c# Windows Application GridView Arrow Keys

    I have a gridView on a Windows form. I need to display contents of a cell in a textbox. I can do it on click of the cell using CellContentClick, but if user uses tab or arrow keys to get to a particular cell then same functionality should hold. I cannot figure out how to capture arrow keys on grid. Please suggest!

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: c# Windows Application GridView Arrow Keys

    There are quite a few events relating to cells.

    The CellContentClick is an interesting one because the click event only fires if you click on the actual contents of the cell. That is on the actual text. The event is not fired if you click on the white space of the cell.

    The CellClick event is fired when you click anywhere in the cell. That is on the actual text or on the whitespace.

    As for your scenario I would consider using the CellEnter event. This will fire when you enter a cell. That is click anywhere in the cell (Same as CellClick) and when you use the arrow keys to navigate to another cell.

    Code:
            private void mainGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                textBox3.Text = mainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    Re: c# Windows Application GridView Arrow Keys

    Well I was able to find the solution to the problem, so thought of sharing. The event is ProcessCmdKey.
    The main challenge for using this event was that I wanted to display the contents of column 7. However, when you tab out of the column if displayed data. In order to resolve that, when columnIndex = 6, display the contents on columnindex+1. similar adjustment for up and down arrow keys.

    Code:
                    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
             {
                 int rowindex = mygridView.CurrentCell.RowIndex;
                 int columnindex = mygridView.CurrentCell.ColumnIndex;
                 if ((keyData == Keys.Tab) || (keyData == Keys.Right) || (keyData == Keys.Left))
                 {
    
                     if (columnindex == 6)
                     {
                         txtFormatMessage.Text = mygridView.Rows[rowindex].Cells[columnindex + 1].Value.ToString();
                     }
                     
                     else
                     {
                         txtFormatMessage.Text = "";
                     }
                 }
                 else if ((keyData == Keys.Up) || (keyData == Keys.Down))
                 {
                     if (columnindex == 7)
                     {
                         txtFormatMessage.Text = mygridView.Rows[rowindex+1].Cells[columnindex].Value.ToString();
                     }
                     else
                     {
                         txtFormatMessage.Text = "";
                     }
                 }
                 return base.ProcessCmdKey(ref msg, keyData);
             }
    [/QUOTE]

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: c# Windows Application GridView Arrow Keys

    OK. For some reason I read it as though you had a beef with the CellContentClick event being unreliable and not always setting the value of the textbox (which can happen when you click in the cell but not on the actual text content) so you had decided to use the arrow keys. Now I see it is deeper than that.

    Although it would have helped to have the additional requirements mentioned about only displaying contents for specific cells.

    However are you sure the code is right for the Left button and Up buttons. The value in the textbox is out by 2 cells. Depending on the direction you are moving in you should probably do a + or - on the column index as in the code below.
    This actually exposed another issue. When you are on the top row and hit the Up button an exception is thrown because the code is trying to read from a none existent row, a row before the first row. The same issue happens when on the last row and the Down button is pressed. The code below checks for these cases.

    There is one more case where the code probably will throw an error. If the cell being read to populate the textbox is the in the last column. Pressing right may throw an error.
    The code below does not check for this case.

    Code:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        string cellText = string.Empty;
        int rowindex = mygridView.CurrentCell.RowIndex;
        int columnindex = mygridView.CurrentCell.ColumnIndex;
    
        if (columnindex == 5 && (keyData == Keys.Tab || keyData == Keys.Right))
        {
            cellText = mygridView.Rows[rowindex].Cells[columnindex + 1].Value.ToString();
        }
        else if (columnindex == 7 && keyData == Keys.Left)
        {
            cellText = mygridView.Rows[rowindex].Cells[columnindex - 1].Value.ToString();
        }
        else if (columnindex == 6 )
        {
            if (keyData == Keys.Down )
            {
                if (rowindex < mygridView.RowCount - 1)
                {
                    cellText = mygridView.Rows[rowindex + 1].Cells[columnindex].Value.ToString();
                }
                else
                {
                    cellText = mygridView.Rows[rowindex].Cells[columnindex].Value.ToString();
                }
            }
            else if (keyData == Keys.Up )
            {
                if (rowindex > 0)
                {
                    cellText = mygridView.Rows[rowindex - 1].Cells[columnindex].Value.ToString();
                }
                else
                {
                    cellText = mygridView.Rows[rowindex].Cells[columnindex].Value.ToString();
                }
            }
        }
                
        txtFormatMessage.Text = cellText;
    
        return base.ProcessCmdKey(ref msg, keyData);
    }

  5. #5
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: c# Windows Application GridView Arrow Keys

    Just as a last note. There are too many magic numbers in this code. If you add a new column before column 7 (or whatever column you are interested in) then the magic number willdneed to change.

    I advise the use of at least one constant

    Code:
    const int SPECIALCOLUMN = 7;
    so in you if statements then use something like the below.
    Code:
    if (columnindex == SPECIALCOLUMN )
    {
    ...
    }
    
    if (columnindex == SPECIALCOLUMN - 1)
    {
    ...
    }
    
    if (columnindex == SPECIALCOLUMN + 1)
    {
    ...
    }
    This way you only have one place to update the column reference.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width