在c#的gridview顶部添加行

时间:2017-02-17 13:47:32

标签: asp.net gridview webforms

如何在Gridview顶部添加新行? 这是代码:

import numpy as np

ds = {('AD', 'TYPE_B', 'TYPE_D'): [np.array([84.0, 85.0, 115.0], dtype=object), 
                                   np.array([31.0, 23.0, 599.0], dtype=object), 
                                   np.array([75.0, 21.0, np.nan], dtype=object), 
                                   np.array([59.0, 52.0, 29.0], dtype=object)],
      ('AD', 'TYPE_A', 'TYPE_N'): [np.array([84.0, 85.0, 115.0], dtype=object), 
                                   np.array([31.0, 23.0, 599.0], dtype=object), 
                                   np.array([75.0, 21.0, 300.0], dtype=object), 
                                   np.array([59.0, 52.0, 29.0], dtype=object)]}

for key in ds.keys():
    #first cast to float and replace nan
    item    = np.nan_to_num(np.asarray(ds[key], dtype=np.float64));
    #calculate the mean
    mean    = np.mean(item, axis=0)
    #store it in the dictionary
    ds[key] = mean

print ds

这是图像: enter image description here 我想在网格的顶部添加项目模板字段中的行..

我想要在网格的顶部,即在第一行的最后一行,我想添加行..任何建议??

1 个答案:

答案 0 :(得分:0)

此示例显示如何在dgv的顶部插入新行(对于绑定dgv,dataSource是一个dataTable):

DataTable table;
public Form1()
{
  InitializeComponent();
  //initializing new dataTable and its columns
  //and setting the dataSource property to dgv:
  table = new DataTable("myTable");
  table.Columns.Add("column 1", typeof(int));
  table.Columns.Add("column 2", typeof(string));
  table.Rows.Add(1, "item 1");
  table.Rows.Add(2, "item 2");
  dataGridView1.DataSource = table;
  dataGridView1.AllowUserToAddRows = false;
}

private void button1_Click(object sender, EventArgs e)
{
  //How to insert new row at the top:
  DataRow dr = table.NewRow();
  table.Rows.InsertAt(dr, 0);
}