0

Try this...
Create Form1, don't change size, add a dataGridView1 and set its anchor to left, top and right, then...

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        dataGridView1.AutoGenerateColumns = true;
        var source = new BindingList<Comic>(GetComics());
        dataGridView1.DataSource = source;
    }

    private List<Comic> GetComics()
    {
        var comics = new List<Comic>();

        comics.Add(new Comic() { id = "1", title = "IronMan", editr = "LayneBooks", pages = 65, owned = true });
        comics.Add(new Comic() { id = "2", title = "The Hulk", editr = "LayneBooks", pages = 48, owned = false });
        comics.Add(new Comic() { id = "3", title = "Superman", editr = "DCCore", pages = 72, owned = true });

        return comics;
    }

    // Custom class, source for grid
    private class Comic
    {
        public string id { get; set; }
        public string title { get; set; }
        public string editr { get; set; }
        public int pages { get; set; }
        public bool owned { get; set; }
    }

As we know, we can go from cell to cell with arrow keys, or with Tab key (when StandardTab = false), and reach the grid corners with Ctrl + arrow keys.

Everything is OK with this code, the cells are focused and selected with keys, but when the columns width are manually changed, the not visible cells are focused, but not showed.

For example, using above code, if the width of first and second columns is reduced to a 20 - 30% of its original size, then, the right corner is not reached while pressing Ctrl + right key.
If the third column width is also reduced, and we try to reach the last one with right keys (or Tab), the cell is focused, but not showed completely (or absolutely).

How to solve this?, change columns width and get the same beahviour as when are autogenerated. Is there any property that I am missing?. My goal is navigate through cells with keys, and show a context menu when Keys.Apps is pressed, but can't do it if the cell it is not visible.

Thanks!

Shin
  • 664
  • 2
  • 13
  • 30

2 Answers2

0

There is no property that solve this behavior.

Please try this code.

public Form1()
{
    InitializeComponent();

    dataGridView1.AutoGenerateColumns = true;
    var source = new BindingList<Comic>(GetComics());
    dataGridView1.DataSource = source;
    dataGridView1.CurrentCellChanged += dataGridView1_CurrentCellChanged;
}

private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
    if (Control.MouseButtons.HasFlag(MouseButtons.Left)) return;
    var dgv = (DataGridView)sender;

    if (dgv.CurrentCell == null) return;

    var dispWidth = dgv.ClientSize.Width - dgv.CurrentRow.HeaderCell.Size.Width;
    int columnsWidthSum = 0;

    for (int i = dgv.CurrentCell.ColumnIndex; i >= 0; i--)
    {
        columnsWidthSum += dgv.Columns[i].Width;

        if (dispWidth >= columnsWidthSum || dispWidth >= dgv.Columns[i].Width)
        {
            dgv.FirstDisplayedScrollingColumnIndex = i;
            break;
        }
    }
}
user3093781
  • 374
  • 3
  • 6
  • thanks, it works but is pretty annoying to click a cell in the middle and that it moves to the beginning of grid. I want to know why is this happening? – Shin Dec 24 '13 at 14:15
  • I added a line to fix the problem with click. – user3093781 Dec 25 '13 at 06:09
-1

Thank you user3093781 very much for your answer. I based my answer on yours.

This code worked for me.

     private void dataGridView_CurrentCellChanged(object sender, EventArgs e) {

        if ( Control.MouseButtons.HasFlag(MouseButtons.Left) )
            return;

        DataGridView dgv = (DataGridView)sender;

        if ( dgv.CurrentCell == null || dgv.Columns.Count == 0 )
            return;

        dgv.FirstDisplayedScrollingColumnIndex =
            dgv.FirstDisplayedScrollingColumnIndex > dgv.CurrentCell.ColumnIndex ?
            dgv.CurrentCell.ColumnIndex :
            dgv.FirstDisplayedScrollingColumnIndex 
            ;
    }
Tanner Ornelas
  • 602
  • 1
  • 8
  • 17