accessing selected row in dataset

  • Thread starter Thread starter paolol
  • Start date Start date
P

paolol

Hi,
I wont to get the current row values from a dataset, any one know how to
reach those data ??
At the moment I use a work around, but it's not good ..
foreach (DataRow row in this.pitagoralavoriDataSet.Addetti.Rows)
{

if (Convert.ToString(row["Nome"])== sNomeU){
idutente = Convert.ToInt32(row["id"]);
}
Because I don't want to show a label with the name ( sNomeU is =
label1.tetx )

Thanks to all.
Paolol
 
paolol,

I assume you mean the currently selected row in a grid? If this is the
case, then what you want to do is get the binding context for the control,
and then pass the data set into it, like so:

// Get the context.
BindingContext context = dataGrid.BindingContext;

// Get the currency manager.
BindingManagerBase manager = context[dataSet, memberName];

// Get the current row view.
DataRowView rowView = (DataRowView) manager.Current;

// Get the row.
DataRow row = rowView.Row;

A few things to point out. The first parameter of the call to the
indexer for BindingContext needs to be the DataSource and the DataMember of
the data source. For example, if you bound the grid to the DataTable
directly, then just use that. If you bound to the DataSet and specified the
table name as the DataMember, then pass the data set and the member name.

You can also access the values in the row using the DataRowView.

Finally, you might just want to bind the text of the label to the
datasource directly. This is easier than setting it yourself when the
current selection changes.

Hope this helps.
 
Thanks Nicholas,
that perfect :)
No I need to get the ID of the row to use in a query that will show in a
new form/grid :)

Paolol

Nicholas Paldino [.NET/C# MVP] ha scritto:
 
Back
Top