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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto return the currentvalue from a datagrid (Silly question...)

Status
Not open for further replies.

RedLion

Programmer
Sep 13, 2000
342
NL
It’s a very silly question, but I can’t get it to work, and I can’t find a solution now I need it. Yes I can get it to work in VB.NET (using the items collector, but C# doesn’t have it…), but in C# it won’t work…

I have pasted a DataTable from a DataSet to a DataGrid.

When someone double clicks a row within a DataGrid I would like to return a value from the column path within the DataTable. So I do the following:

Code:
return ((DataTable)grdResult.DataSource).Rows[grdResult.CurrentRowIndex][1].ToString();

And then I got the following error:
Code:
error: object '(DataTable)grdResult.DataSource.Rows' doesn't have an indexer

What stupid mistake have I made?

Thanks!
 
try referencing the value in the cell:

Code:
return grdResult[grdResult.CurrentRowIndex, 0].ToString();

this will return the value in the first cell of the current row.



mr s. <;)

 
int iActiveCellRow=grdResult.ActiveRow.Index;
and iActiveCellRow maps to yourDataTable.Rows[ index]
 
Thanks for the responses, I will go and try it.

But still the question, isn't it possible to access a field in a DataTable by just referencing a DataTable?

Code:
return myDataTable.Rows[0]["path"].toString();

or something like that?
 
yes u can,i thought you wanted the cuurent row and cell values:)
 
Yes chmohan, you're right, but because it didn't work in the first try out, I tried a couple of things, and one was
Code:
return myDataTable.Rows[0]["path"].toString();
and that resulted in the error that "doesn't have an indexer" on the "Rows" object, so I was curious if it's possible or not? But the code example doesn't work here because the exception says that "Rows[0]" may not be done...

Any inside information I'm not aware of???
(That's the reason for mine second question)
 
try getting the rows.count and see
also you may try Rows[0][0].ToString() to see if u can get the first columsn value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top