-
Oct 22nd, 2009, 12:00 AM
#1
Thread Starter
Lively Member
[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.
-
Oct 22nd, 2009, 12:19 AM
#2
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
-
Oct 22nd, 2009, 12:26 AM
#3
Thread Starter
Lively Member
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].
-
Oct 22nd, 2009, 12:29 AM
#4
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
-
Oct 22nd, 2009, 12:31 AM
#5
Thread Starter
Lively Member
Re: Focus to a specific cell in dataGridView
i want the text selected in the current row and cell[4].
-
Oct 22nd, 2009, 12:38 AM
#6
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
-
Oct 22nd, 2009, 01:02 AM
#7
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:
myDataGridView.CurrentCell = myDataGridView(2, 5)
-
Oct 22nd, 2009, 01:18 AM
#8
Thread Starter
Lively Member
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?
-
Oct 22nd, 2009, 01:22 AM
#9
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.
-
Oct 22nd, 2009, 01:34 PM
#10
Thread Starter
Lively Member
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.
-
Oct 22nd, 2009, 06:11 PM
#11
Re: Focus to a specific cell in dataGridView
I don't see anywhere in that code that you're setting the CurrentCell.
-
Sep 17th, 2013, 02:54 AM
#12
Registered User
Re: Focus to a specific cell in dataGridView
Originally Posted by jmcilhinney
I don't see anywhere in that code that you're setting the CurrentCell.
the "CurrentCell" property is read-only! XD
-
Sep 17th, 2013, 05:50 AM
#13
Re: Focus to a specific cell in dataGridView
Originally Posted by tieraerde03
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:
Originally Posted by MSDN
Gets or sets the currently active cell.
This is from the documentation for the CurrentRow property:
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.
-
Oct 3rd, 2024, 11:34 AM
#14
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|