dataGridView設置方法

string Str=datagridview1[Column,Row].value.tostring();//Column列號Row行號
驗證是否選中行 
datagridview1.SelectedRows.Count>0 
設置行標題列
dataGridView1.Rows[i].HeaderCell.Value = "第一行";
隱藏首行
dataGridView1.ColumnHeadersVisible = false;
隱藏首列
dataGridView1.RowHeadersVisible = false;
顏色控制
dataGridView1[i, y].Style.ForeColor = System.Drawing.Color.Red;
設置行字體顏色
dataGridView1.Rows [i]. DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
單元格對齊方式
 dataGridView1.Rows[i].Cells[12].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
獲取當前行號
 dataGridView1.CurrentCell.RowIndex
string xx = dataGridView1.CurrentCell.RowIndex.ToString ();//行號
string xx = dataGridView1.CurrentCell.ColumnIndex . ToString();//列號  
刪除當前行 
dataGridView1.Rows.Remove(dataGridView1.CurrentRow)
增加行
  dataGridView1.Rows.Add(1)
獲取總行數
dataGridView1.Rows.Count 
獲取總列數
dataGridView1.ColumnCount
獲取列名
dataGridView1.Columns[i].HeaderText

禁止第i列排序
dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;

插入行 插入到第x行後 插入y行
dgv.Rows.Insert(x, y);  
設置當前行
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];

列寬自適應
dataGridView1 . Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
單元格對齊方式
dataGridView1.Rows[0].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleLeft ;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章