在winforms网格视图中选择行

时间:2013-07-02 08:21:27

标签: c# .net winforms

我的gridview填充了像这样的文章对象

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;

我很想知道如何识别此网格的选定行,例如在LabelSelectedRow.Text上应填充选定的行代码文本。

1 个答案:

答案 0 :(得分:1)

首先,您可以获得所选行;

//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];

编辑:您可以将此代码放在其中一个datagrid点击事件中,可能是RowSelected。