panjo
panjo

Reputation: 3515

select row in winforms grid view

My gridview is populated with Article object like this

var sel = (Article)cmbArticleList.SelectedItem;

 DataRow newRow = articlesTable.NewRow();
 newRow["CODE"] = sel.Code;
 newRow["NAME"] = sel.Name;
 newRow["PRICE"] = sel.Price;

 articlesTable.Rows.Add(newRow);
 articlesGridView.DataSource = articlesTable;

I'm wonder how can I recognize selected row of this grid, for example on LabelSelectedRow.Text should be populated with selected row Code text.

Upvotes: 0

Views: 2336

Answers (1)

Kestami
Kestami

Reputation: 2073

First off you can get your selected rows like so;

//For multiple row selection.
IList rows = dg.SelectedItems;

//For single row selection;
DataRowView row = (DataRowView)dg.SelectedItems[0];

//You can then access them via their column name;
row["yourColumnName"];

//So to assign to say your label..
LabelSelectedRow.Text = row["CODE"] + " " + " row[NAME] + " " + row[PRICE];

Edit: You can put this code in one of the datagrid click events, perhaps RowSelected.

Upvotes: 1

Related Questions