2014-03-19 27 views
-1

我有一個WinForm應用程序,並且在此窗體上我有以下控件。將窗口應用程序中的第一行值複製到GridView上的其他行表單應用程序

1-Grid view 
2-Copy button 

- 如何在網格視圖的第一行旁僅放置一個按鈕(複製按鈕)? - 點擊複製按鈕後,如何填充第一行並使其他16行與我的第一行相同?但唯一不同的是獲得遞增值的ID。

例子:

ID Explanation Teacher_ID 
--- ----------- ---------- 
0 hello  45   CopyButton 
1 hello  45 
2 hello  45 
3 hello  45 
. .   . 
. .   . 
. .   . 
16 hello  45 

回答

0

創建一個查詢類

public class LookUp 
    { 
     public long Id { get; set; } 
     public string Explanation { get; set; } 
     public long TeacherId { get; set; } 
    } 

在點擊按鈕

private void ClickEvent() 
    { 
     //Use correct columnId or name here 
     var name = dataGridView1.Rows[1].Cells[2] != null ? dataGridView1.Rows[1].Cells[2].ToString() : ""; 
     var id = dataGridView1.Rows[1].Cells[3] != null ? long.Parse(dataGridView1.Rows[1].Cells[3].ToString()) : 0; 
     var datalist = GetList(name, id, 16); //you have a copy of values with diffrent ids -- rest is for you 
    } 

    //create a method which return copy of values but with different id 
    private IEnumerable<LookUp> GetList(string name, long id,int till) 
    { 
     for (var i = 1; i <= till; i++) 
     { 
      yield return new LookUp() { Id = i, Explanation = name,TeacherId = id};      
      } 
    } 
+0

錯誤:變種名稱= .....(名爲2列不能找不到)名爲2的列無法找到。 參數名稱:columnName – user2827565

+0

這只是一個例子....所以我猜對了列ID,使用實際的列ID作爲'Explanation「和'Teacher_ID'列或列名來獲取值! –

+0

輸入字符串格式不正確。現在出現錯誤 – user2827565

相關問題