2011-03-18 53 views
3

出於好奇,是否可以根據數據網格中的行選擇打開表單?我還需要表單來顯示基於數據網格中用戶名的信息。個人用戶名包含在數據網格的行內。根據數據網格中的行選擇打開表單

+0

你是嗎詢問是否有一些「自動」的方式來做到這一點?或者如果它可能呢? – CodingGorilla 2011-03-18 16:19:20

+0

好吧,如果可能的話,ive有一看,我曾經在數據網格上找到很多信息。 – 2011-03-18 16:22:44

+0

@JoeBell當你說打開一個包含詳細信息的表單時,是否可以像使用所需人員的用戶名鏈接到另一個頁面一樣簡單,還是想要捕獲ItemCommand上的選擇,然後對所選人員進行操作? – gdp 2011-03-18 16:40:15

回答

2

您可以在下面的事件在處理這個

dataGridView1_CellClick 

獲取datagridiview

檢查爲username存在或不按你問的CurrentCell值並顯示相應的形式

示例代碼:

if (this.dataGridView1.CurrentCell != null) 
{ 
    string strusrname=dataGridView1.CurrentCell.Value.ToString(); 
    //Here find out for the user name from the string as you get the currentcell value of the datagridview 
    // Raise the corresponding form as per you required 
} 
+0

當單擊單元格的任何部分時,即使邊框和填充都不會發生這種情況嗎?你會想要使用DataGridView1_CellContentClick。 – gdp 2011-03-22 09:52:31

+0

@Geppie對我有用我已經在這個 – Dotnet 2011-03-22 09:55:58

+0

@Geppie上完成了一個程序:但是因爲我只有一個單元格完全帶有文本,所以我用這個可能會改變,如果他有不同的單元格,那麼他會像你說的那樣去做 – Dotnet 2011-03-22 09:58:22

4

你將不得不編碼這個,但是,是的,這是可能的。

首先,用你可以處理的數據填充你的DataGrid。

在DataGrid的Selection Changed事件上,讀取該數據,創建要顯示的表單(如果它尚不存在),並使用Show()顯示它。

這將像一個典型的菜單程序。

0

不太確定這是否是你之後的事情,因爲如果你想在另一個預先構建的表單上顯示數據或創建一個新數據,但不是這樣,但是在這裏。 這樣,你甚至不需要擔心選擇的行,假設你有綁定到DataGrid的人的用戶名,你可以創建一個hyperlinkcolumn這樣的:

<asp:HyperLinkcolumn DataNavigateUrlField="Username" 
        DataNavigateUrlFormatString="PersonForm.aspx?Username={0}"  
        HeaderText="More Details" 
        Text="View Person Details" /> 

然後爲personForm可以加載人細節。或者,如果您希望獲得關於如何在itemcommand上捕獲所選行的幫助,請不要。

希望這會有所幫助。

編輯:你的WinForms標籤更新後,你可能有興趣在此:DataGridViewLink On MSDN

一般的代碼是:

DataGridViewLinkColumn links = new DataGridViewLinkColumn(); 

links.UseColumnTextForLinkValue = true; 
links.HeaderText = ColumnName.ReportsTo.ToString(); 
links.DataPropertyName = //Set your field here. 
links.ActiveLinkColor = Color.White; 
links.LinkBehavior = LinkBehavior.SystemDefault; 
links.LinkColor = Color.Blue; 
links.TrackVisitedState = true; 
links.VisitedLinkColor = Color.YellowGreen; 

DataGridView1.Columns.Add(links); 

一旦你添加了一個鏈接,您使用DataGridView1_CellContentClick可以趕上它,做你想做的事,即打開一個新的表格或改變當前的表格。

+0

他要求Winforms不適用於Web應用程序 – Dotnet 2011-03-22 09:33:54

+0

@Dorababu是的,當問題仍然模糊時,第一個Web應用程序部分最初在那裏。我只是在我的編輯中添加了windows.forms代碼smaple和鏈接。 – gdp 2011-03-22 09:40:30