[.net+Winform]DataGridView常用的語法

  • 1195
  • 0

[.net+Winform]DataGridView常用的語法

常用的語法請參考:
 

//設定datagridview的資料來源為datatable
gvChkList.DataSource = dtWebsites;
//datagridview隱藏不必要顯示的欄位
gvChkList.Columns["SourceCode"].Visible = false;
gvChkList.Columns["EventType"].Visible = false;
gvChkList.Columns["Status"].Visible = false;
gvChkList.Columns["UpdateTime"].Visible = false;
gvChkList.Columns["Enabled"].Visible = false;

//datagridview設定欄位的排列順序
gvChkList.Columns["SourceName"].DisplayIndex = 0;
gvChkList.Columns["EventTypeName"].DisplayIndex = 1;
gvChkList.Columns["url"].DisplayIndex = 2;


//datagridview設定欄位長度
gvChkList.Columns["url"].Width = 215;

//datagridview加入checkbox
DataGridViewCheckBoxColumn dgvc = new DataGridViewCheckBoxColumn();
dgvc.Width = 40;
dgvc.Name = "選取";
dgvc.TrueValue = true;
dgvc.FalseValue = false;
this.gvChkList.Columns.Insert(0, dgvc);

//打勾datagridview所有的checkbox
foreach (DataGridViewRow row in gvChkList.Rows)
{
	if (row.Cells["SourceCode"].Value != null)
	{
		DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
		chk.Value = chk.TrueValue;


	}

}

//設定datagridview欄位是否唯讀
gvChkList.Columns["選取"].ReadOnly = false;
gvChkList.Columns["SourceName"].ReadOnly = true;
gvChkList.Columns["EventTypeName"].ReadOnly = true;
gvChkList.Columns["url"].ReadOnly = true;


//禁止user新增資料
gvChkList.AllowUserToAddRows = false;

//不顯示header
gvChkList.ColumnHeadersVisible = false;

//設定背景顏色
gvChkList.BackgroundColor = Color.White;