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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章