在控件DataGridView綁定數據源后,發現DataGridViewCheckBoxColumn不能顯示當前的check值。經過一番努力,現將完整代碼奉獻出來,僅供參考。
錯誤代碼:
/*禁止自動創建Column*/
this.dgvTestType.AutoGenerateColumns = false;
/*設置binding 屬性值*/
this.dgvTestType.Columns[0].DataPropertyName ="Id";
this.dgvTestType.Columns[1].DataPropertyName = "TestType";
this.dgvTestType.Columns[2].DataPropertyName = "Name";
this.dgvTestType.Columns[3].DataPropertyName ="TestDynamic"; //Check列
this.dgvTestType.Columns[4].DataPropertyName ="TestSynch";//Check列
this.dgvTestType.Columns[5].DataPropertyName = "Description";
/*獲取數據源*/
DataSet ds = SqliteSingleModel.Instance.ReadMethod("select [Id],[TestType],[Name],[TestDynamic],[TestSynch],[Description] from config_testtype order by TestType");
/*binding 數據源*/
if (ds.Tables[0] != null)
{
this.dgvTestType.DataSource = ds.Tables[0];
}
執行后:
增加正確代碼:
/*TestDynamic屬性*/
DataGridViewCheckBoxColumn checkboxDynamic = this.dgvTestType.Columns[3] as DataGridViewCheckBoxColumn;
checkboxDynamic.TrueValue = "True";
checkboxDynamic.FalseValue = "False";
/*TestSynch屬性*/
DataGridViewCheckBoxColumn checkboxSynch = this.dgvTestType.Columns[4] as DataGridViewCheckBoxColumn;
checkboxSynch.TrueValue = "True";
checkboxSynch.FalseValue = "False";
運行結果: