none
VB.NET 的DataGridView 里面有一列是CheckBox RRS feed

  • 问题

  •  在DataGrdiView里面 ,有一列是CheckBox,怎么实现在 点击 该CheckBox 的单元格(是单元格不是CheckBox的小方框,是小方框以外的部分),就让该单元格里面的CheckBox 打勾 或者是打勾去掉 ? 急!急!急! 

            由于小方框太小,所有要实现 在点击单元格的时候,CheckBox也要打勾或者打勾去掉。

    2015年6月18日 9:41

答案

  • 您好,

    或許可以在 DataGridView 的 CellClick 事件裡處理,類似如下,

    private void Form1_Load(object sender, EventArgs e)
    {
    	DataTable dt = new DataTable("t1");
    	dt.Columns.Add("c1", typeof(bool));
    	dt.Columns.Add("c2", typeof(string));
    	dt.Rows.Add(true, "c1");
    	dt.Rows.Add(false, "c2");
    	this.dataGridView1.DataSource = dt;
    	
    }
    
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    	if (e.RowIndex > -1 && e.ColumnIndex == 0)
    	{
    		DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
    		dataGridView1.BeginEdit(true);
    		bool isChecked = Convert.ToBoolean( cell.Value);
    		cell.Value = !isChecked;
    		dataGridView1.EndEdit();
    	}
    }


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2015年6月21日 5:43