将第一行值复制到Windows窗体应用程序中的GridView上的其他行

时间:2014-03-19 15:44:31

标签: c# winforms

我有一个WinForm应用程序,在此表单上我有以下控件。

1-Grid view
2-Copy button

- 如何在网格视图的第一行旁边只放置一个按钮(复制按钮)? - 单击复制按钮后,如何填充第一行并使其他16行与我的第1行相同?但只有不同的ID才能获得递增的值。

示例:

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

1 个答案:

答案 0 :(得分: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};                    
          }
     }