Results 1 to 14 of 14

Thread: [RESOLVED] Focus to a specific cell in dataGridView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Resolved [RESOLVED] Focus to a specific cell in dataGridView

    My question is if i have a dataGridView called inventoryItemsDataGridView how can I in code cause it to focus and select the text in the cell of a specific cell in the datagrid.
    Last edited by new2visualBasic; Oct 22nd, 2009 at 12:10 AM.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Focus to a specific cell in dataGridView

    Are you looking this for one ?

    Code:
    dataGridView1.Rows[0].Cells[1].Selected = true;
    The above code will select the First rows second cell
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Re: Focus to a specific cell in dataGridView

    here is the code to help find my answer
    HTML Code:
    foreach (DataGridViewRow row in endingInventoryDataGridView.Rows)
                {
                    DataGridViewCell itemIDcell = row.Cells[1];
                    DataGridViewCell endingCountCell = row.Cells[4];
                    DataGridViewCell itemNameCell = row.Cells[2];
                    int itemID = (int)itemIDcell.Value;
                    string itemName = (string)itemNameCell.Value;
                    decimal endingCount = (decimal)endingCountCell.Value;
    
                    decimal beginningCount = EndingInventoryDB.GetBeginningCount(itemID);
    
                    if (beginningCount < endingCount)
                    {
                        MessageBox.Show("Ending quantity for " + itemName + " must be less than or equal to " + beginningCount,
                                        "Ending Quanity Error");                    
                        endingQuantityError = true;
                       
                        endingInventoryDataGridView.Rows[row].Cells[4].Selected = true;                    
                        break;
                    }
                }
    I'm not sure then what to but where i have [row].

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Focus to a specific cell in dataGridView

    Where do you want to set the selection ?
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Re: Focus to a specific cell in dataGridView

    i want the text selected in the current row and cell[4].

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Focus to a specific cell in dataGridView

    Change this one to

    Code:
    endingInventoryDataGridView.Rows[row].Cells[4].Selected = true;
    Code:
    row.Cells[4].Selected = true;
    This will set the
    Please mark you thread resolved using the Thread Tools as shown

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,775

    Re: Focus to a specific cell in dataGridView

    Setting the Selected property of a DataGridViewCell doesn't give it focus. It simply highlights it. It's possible to have multiple cells selected while only one cell can have focus. What you need to do is set the CurrentCell property of the grid, e.g.
    Code:
    1. myDataGridView.CurrentCell = myDataGridView(2, 5)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Re: Focus to a specific cell in dataGridView

    this is what I tried but causes an error:
    HTML Code:
    endingInventoryDataGridView.CurrentCell = endingInventoryDataGridView(e.RowIndex, 4);
    e.rowIndex would be the row and 4 would be the column, correct?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,775

    Re: Focus to a specific cell in dataGridView

    If you check the documentation for the DataGridView.Item property you'll see that the column comes before the row. Intellisense would also have told you that when you typed the opening parenthesis.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Re: Focus to a specific cell in dataGridView

    I am still having the difficulty getting this to work. Here is my code:
    Code:
    foreach (DataGridViewRow row in endingInventoryDataGridView.Rows)
                {
                    DataGridViewCell itemIDcell = row.Cells[1];
                    DataGridViewCell endingCountCell = row.Cells[4];
                    DataGridViewCell itemNameCell = row.Cells[2];
                    int itemID = (int)itemIDcell.Value;
                    string itemName = (string)itemNameCell.Value;
                    decimal endingCount = (decimal)endingCountCell.Value;
    
                    decimal beginningCount = EndingInventoryDB.GetBeginningCount(itemID);
    
                    if (beginningCount < endingCount)
                    {
                        MessageBox.Show("Ending quantity for " + itemName + " must be less than or equal to " + beginningCount,
                                        "Ending Quanity Error");
                        
                        return false;                         
                    }
                }
    When ever this error is encountered I want to focus and select the text is the cell causing the error. I have tried the examples but still haven't seemed to work.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,775

    Re: Focus to a specific cell in dataGridView

    I don't see anywhere in that code that you're setting the CurrentCell.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    Re: Focus to a specific cell in dataGridView

    Quote Originally Posted by jmcilhinney View Post
    I don't see anywhere in that code that you're setting the CurrentCell.
    the "CurrentCell" property is read-only! XD

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,775

    Re: Focus to a specific cell in dataGridView

    Quote Originally Posted by tieraerde03 View Post
    the "CurrentCell" property is read-only! XD
    I'm afraid that you've made a rather inauspicious debut. You've resurrected a thread that is nearly four years old and you've done it with false information. The CurrentRow and CurrentCellAddress properties are read-only but the CurrentCell property is not. Here's how the MSDN Library describes that property:
    Quote Originally Posted by MSDN
    Gets or sets the currently active cell.
    This is from the documentation for the CurrentRow property:
    Quote Originally Posted by MSDN
    To change the current row, you must set the CurrentCell property to a cell in the desired row.
    So, better luck next time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    New Member
    Join Date
    Oct 2024
    Posts
    1

    Re: [RESOLVED] Focus to a specific cell in dataGridView

    cl = DataGridView1.CurrentCell.ColumnIndex
    rw = DataGridView1.CurrentRow.Index
    DataGridView1.Select()
    DataGridView1.Rows(rw).Cells(cl).Selected = True
    DataGridView1.CurrentCell = DataGridView1(cl, rw)

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