C#GridView行添加在错误的地方?

时间:2018-10-31 20:19:35

标签: c# gridview row add

我想在函数运行时向GridView添加行。

在添加行的函数被调用之前,上一个函数始终存在1个“ DATA”行。

要添加我尝试使用的行:

f_dir = '/project/cper_neon_aop/regionalization/data/nlcd/nlcd_2011_landcover_2011_edition_2014_10_10/'
nlcd_f = 'nlcd_2011_landcover_2011_edition_2014_10_10.img'
from rasterio.vrt import WarpedVRT as rio_WarpedVRT
with rasterio.open(f_dir+nlcd_f) as src:
    print(src.profile)
    with rio_WarpedVRT(src,transfrom=rasterio.Affine(*dat_cdl['CDL_Data'].attrs['transform']),crs=dat_cdl['CDL_Data'].attrs['crs'],height=dat_cdl['CDL_Data'].shape[0],width=dat_cdl['CDL_Data'].shape[1],NUM_THREADS=2,warp_mem_limit=1024,) as vrt:    
        rio_shutil.copy(vrt, f_dir+nlcd_f[0:-4]+'_cdl_grid.vrt', driver='VRT')
        print(vrt.profile)
        print(vrt.shape)

{'driver': 'HFA', 'dtype': 'uint8', 'nodata': None, 'width': 161190, 'height': 104424, 'count': 1, 'crs': CRS({'proj': 'aea', 'lat_1': 29.5, 'lat_2': 45.5, 'lat_0': 23, 'lon_0': -96, 'x_0': 0, 'y_0': 0, 'ellps': 'GRS80', 'towgs84': '0,0,0,-0,-0,-0,0', 'units': 'm', 'no_defs': True}), 'transform': Affine(30.0, 0.0, -2493045.0,
       0.0, -30.0, 3310005.0), 'blockxsize': 64, 'blockysize': 64, 'tiled': True}
{'driver': 'VRT', 'dtype': 'uint8', 'nodata': None, 'width': 161190, 'height': 104424, 'count': 1, 'crs': CRS({'ellps': 'GRS80', 'lat_0': 23, 'lat_1': 29.5, 'lat_2': 45.5, 'lon_0': -96, 'no_defs': True, 'proj': 'aea', 'towgs84': '0,0,0,-0,-0,-0,0', 'units': 'm', 'x_0': 0, 'y_0': 0}), 'transform': Affine(30.0, 0.0, -2493045.0,
       0.0, -30.0, 3310005.0), 'blockxsize': 512, 'blockysize': 128, 'tiled': True}
(104424, 161190)

并且我希望获得带有标签的行:

int n = grid.Rows.Count;    
grid.Rows.Add("Step" + n.ToString());

相反,我得到了:

Data...    
Step1...
Step2...
Step3...
etc

(DATA行被推到最底部,所有行都被添加到它的上方) 我的错误在哪里?

编辑: 我尝试过:

Step1...
Step2...
Step3...
etc
Data...

但它会出错。

2 个答案:

答案 0 :(得分:0)

您可以创建DataTable,然后在GridView上显示它! 试试这个:

System.Data.DataTable table = new DataTable();
table.Columns.Add("col1", typeof(string));
table.Columns.Add("col2", typeof(string));
table.Columns.Add("col3", typeof(string));
...

这将创建行:

for (int i = 0; i < n - 1; i++)
        {
            DataRow dr = table.NewRow();
            if(i==0)
               {
                  dr[col1]="Data"
                  dr[col2]= ...
                  dr[col3]= ...
                  ...
               }
             if(i>0)
               {
                  // your code
               }
            table.Rows.Add(dr);
         }

将此表添加到GridView:

DataView dv1 = new DataView(table);
dataGridView1.DataSource = dv1;

答案 1 :(得分:0)

使用Insert添加记录,并使用Count添加最后一个位置

类似的东西:

grid.Rows.Insert(grid.Rows.Count-1, row);
相关问题