簡體   English   中英

在winforms網格視圖中選擇行

[英]select row in winforms grid view

我的gridview像這樣填充Article對象

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應該填充選定的行Code文本。

首先,您可以像這樣獲得選定的行;

//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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM