winform datagridview 动态添加行或列

动态添加行

                  //首先 声明一个 DataGridViewRow 对象 (即要添加的行)

                  DataGridViewRow dr = new DataGridViewRow();   

                //设置要添加行的列

              DataGridViewCheckBoxCell check=new DataGridViewCheckBoxCell();
                  DataGridViewTextBoxCell text=new DataGridViewTextBoxCell();
                  dr.Cells.Add(check);
                  dr.Cells.Add(text);

              //设置列的值
                  dr.Cells[1].Value = "aaaa";

              //将声明的行添加到dataGridView1 中

                 dataGridView1.Rows.Add(dr);

         /////////////////////////////////////////////////////////////////////////////////

        //添加列

   DataGridViewColumn dc = new DataGridViewColumn();
    
   dc.DataPropertyName = "FID";
    dc.Visible = false;
  dc.SortMode = DataGridViewColumnSortMode.NotSortable;
   dc.HeaderText = "唯一ID";
  dc.CellTemplate = null;
  this.dgv_StockList.Columns.Add(dc);

 

你可能感兴趣的:(WinForm,datagridview,动态添加行或列)