Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Establishing current row in DataGrid / DataTable

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
How do I determine the current row that I am focused on in a DataGrid component (and hence the contents of that row) ?

We've made use of the HitTest but this only seems to be effective when we select the row through means of a MouseClick. We need to allow for the fact that the user may have clicked on it, cursor'ed up/down/left/right, Page Up/Down, Ctrl+Home/Ctrl+End, etc.

Ideally I want to have a button that I click and it reads back to me the contents of the current row.

I'm guessing this is easily done but I can't see it for looking.

Any help would be appreciated.
Thanks in advance

Steve
 
I've now solved this issue making use of the syntax :

int currRow = dataGrid1.CurrentCell.RowNumber;
MessageBox.Show(currRow.ToString());

The issue I now have is that I get a reply of 0 if I am either on the first row in the grid (index starts at 0) or if I have an empty grid.

How can I determine whether or not the grid has any entries on display ?

Thanks again.
Steve
 
I've tried ideas along that line but there is no dataGrid1.Rows.Count

I'm looking at using something like :

DataTable dt = (DataTable)dataGrid1.DataSource;
if (dt.Rows.Count == 0)
{
// Do something
}
else
{
// Cannot do something
}

The problem with this is that sometimes the conversion to a DataTable does not work as I am sometimes using DataSets and DataRelations through something like :
dataGrid2.SetDataBinding(dataSet1, "Table1");

Is there a more generic conversion I might look at making use of ?
Or should I put try ... catch's around code blocks for various 'DataSource's that sit behind the DataGrids ?

Any further thoughts ?

Thanks again.
Steve
 
How about this to see if there is at least 1 row:

MessageBox.Show(dataGrid1.VisibleRowCount.ToString());
 
This is something I considered but (playing devil's advocate) I'm also allowing the components to be resized (when the form is resized) so may well run the risk of having 0 visible rows, albeit that the user has selected a grid entry and then squashed up the grid.

Is there an alternative way that can be suggested ?

Steve

(Maybe I'm just being awkward now ...)
 
Yeah, I assume you have a method that the form load calls to rezie and hide columns. Before you want to check for visible rows, call a method which does the oppisite--unhides everything. Then immediately after you get the count, call the hide method again. It would probably be virtually unnoticeable to the user.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top