C# Winform中DataGridView綁定后DataGridViewCheckBoxColumn無法顯示的問題


在控件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";

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM