未绑定DataGridViewCheckBoxCell设置值 [英] Unbound DataGridViewCheckBoxCell set value

查看:168
本文介绍了未绑定DataGridViewCheckBoxCell设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我的代码包含DataGridView和UnBound CheckBoxColumn。
我的对话框应该是从列表中选择房间。
第二次你想看看你第一次选择了什么。



代码:



ChooseGroupRoomDialog.cs

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Drawing;
使用System.Data;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用BE; //我的命名空间
使用ListExtensions; //我的命名空间

命名空间PLForms
{
public partial class ChooseGroupRoomsDialog:Form
{
public List< Room>价值
{
get
{
列表<房间> retList = new List< Room>(); (int i = 0; i< table.RowCount; i ++)

DataGridViewCheckBoxCell cell =(DataGridViewCheckBoxCell)table [chooseColumnIndex,i];
if(cell.Value!= null&&(bool)cell.Value)
retList.Add((Room)table.Rows [i] .DataBoundItem);
}
return retList;
}
set
{
列表<房间> data =(List< Room>)(table.DataSource);
foreach(房间房间)
{
int index = data.FindIndex(roomA => roomA.Id == room.Id);
if(index!= -1)
{
table [index,chooseColumnIndex] .Value = true; //不工作!!
}
}
}
}
private int chooseColumnIndex;
public ChooseGroupRoomsDialog(List< Room> dataSource)
{
InitializeComponent();
table.DataSource = dataSource.ToList();
DialogResult = DialogResult.Cancel;
foreach(DataGridViewColumn col in table.Columns)
col.ReadOnly = true;
chooseColumnIndex = chooseColumn.Index;
chooseColumn.ReadOnly = false;
}

private void okButton_Click(object sender,EventArgs e)
{
DialogResult = DialogResult.OK;
this.Close();
}

private void cancelButton_Click(object sender,EventArgs e)
{
this.Close();
}
}
}

ChooseGroupRoomDialog.Designer.cs

 命名空间PLForms 
{
部分类ChooseGroupRoomsDialog
{
///< ;摘要>
///必需的设计器变量。
///< / summary>
private System.ComponentModel.IContainer components = null;

///< summary>
///清理正在使用的资源。
///< / summary>
///< param name =disposal>如果管理资源应该处理,则为true;否则为false。< / param>
protected override void Dispose(bool disposal)
{
if(disposal&&(components!= null))
{
components.Dispose();
}
base.Dispose(处置);
}

#region Windows窗体设计器生成的代码

///< summary>
/// Designer支持的必需方法 - 不要使用代码编辑器修改
///该方法的内容。
///< / summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.table = new System.Windows.Forms.DataGridView();
this.roomBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.okButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.chooseColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.numberOfBedsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.typeOfRoomStringDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.viewingSeaDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.costDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.jakuziDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.numberOfWindowsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.airConditionedDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.cleaningDateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.landScapeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.table))。BeginInit();
((System.ComponentModel.ISupportInitialize)(this.roomBindingSource))。BeginInit();
this.SuspendLayout();
//
// table
//
this.table.AllowUserToAddRows = false;
this.table.AllowUserToDeleteRows = false;
this.table.Anchor =((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|系统.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.table.AutoGenerateColumns = false;
this.table.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.table.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn [] {
this.chooseColumn,
this.idDataGridViewTextBoxColumn,
this.numberOfBedsDataGridViewTextBoxColumn,
this.typeOfRoomStringDataGridViewTextBoxColumn,
this.viewingSeaDataGridViewCheckBoxColumn,
this.costDataGridViewTextBoxColumn,
this.jakuziDataGridViewCheckBoxColumn,
this.numberOfWindowsDataGridViewTextBoxColumn,
this.airConditionedDataGridViewCheckBoxColumn,
this .cleaningDateDataGridViewTextBoxColumn,
this.landScapeDataGridViewTextBoxColumn});
this.table.DataSource = this.roomBindingSource;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle1.Font = new System.Drawing.Font(Microsoft Sans Serif,8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(177)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.GradientInactiveCaption;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.table.DefaultCellStyle = dataGridViewCellStyle1;
this.table.Location = new System.Drawing.Point(129,12);
this.table.MultiSelect = false;
this.table.Name =table;
this.table.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.table.Size = new System.Drawing.Size(1047,372);
this.table.TabIndex = 1;
//
// roomBindingSource
//
this.roomBindingSource.DataSource = typeof(BE.Room);
//
// okButton
//
this.okButton.Location = new System.Drawing.Point(12,12);
this.okButton.Name =okButton;
this.okButton.Size = new System.Drawing.Size(111,44);
this.okButton.TabIndex = 2;
this.okButton.Text =אישור
this.okButton.UseVisualStyleBackColor = true;
this.okButton.Click + = new System.EventHandler(this.okButton_Click);
//
// cancelButton
//
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(12,62);
this.cancelButton.Name =cancelButton;
this.cancelButton.Size = new System.Drawing.Size(111,44);
this.cancelButton.TabIndex = 2;
this.cancelButton.Text =ביטול;
this.cancelButton.UseVisualStyleBackColor = true;
this.cancelButton.Click + = new System.EventHandler(this.cancelButton_Click);
//
// chooseColumn
//
this.chooseColumn.HeaderText =בחירה;
this.chooseColumn.Name =chooseColumn;
this.chooseColumn.Width = 70;
//
// idDataGridViewTextBoxColumn
//
this.idDataGridViewTextBoxColumn.DataPropertyName =Id;
this.idDataGridViewTextBoxColumn.HeaderText =מספרזיהוי;
this.idDataGridViewTextBoxColumn.Name =idDataGridViewTextBoxColumn;
//
// numberOfBedsDataGridViewTextBoxColumn
//
this.numberOfBedsDataGridViewTextBoxColumn.DataPropertyName =NumberOfBeds;
this.numberOfBedsDataGridViewTextBoxColumn.HeaderText =מספרמיטות;
this.numberOfBedsDataGridViewTextBoxColumn.Name =numberOfBedsDataGridViewTextBoxColumn;
//
// typeOfRoomStringDataGridViewTextBoxColumn
//
this.typeOfRoomStringDataGridViewTextBoxColumn.DataPropertyName =TypeOfRoomString;
this.typeOfRoomStringDataGridViewTextBoxColumn.HeaderText =סוגחדר;
this.typeOfRoomStringDataGridViewTextBoxColumn.Name =typeOfRoomStringDataGridViewTextBoxColumn;
this.typeOfRoomStringDataGridViewTextBoxColumn.ReadOnly = true;
//
// viewerSeaDataGridViewCheckBoxColumn
//
this.viewingSeaDataGridViewCheckBoxColumn.DataPropertyName =ViewingSea;
this.viewingSeaDataGridViewCheckBoxColumn.HeaderText =נוףלים;
this.viewingSeaDataGridViewCheckBoxColumn.Name =viewingSeaDataGridViewCheckBoxColumn;
this.viewingSeaDataGridViewCheckBoxColumn.Width = 70;
//
// costDataGridViewTextBoxColumn
//
this.costDataGridViewTextBoxColumn.DataPropertyName =Cost;
this.costDataGridViewTextBoxColumn.HeaderText =מחיר;
this.costDataGridViewTextBoxColumn.Name =costDataGridViewTextBoxColumn;
this.costDataGridViewTextBoxColumn.ReadOnly = true;
//
// jakuziDataGridViewCheckBoxColumn
//
this.jakuziDataGridViewCheckBoxColumn.DataPropertyName =Jakuzi;
this.jakuziDataGridViewCheckBoxColumn.HeaderText =ג\'קוזי;
this.jakuziDataGridViewCheckBoxColumn.Name =jakuziDataGridViewCheckBoxColumn;
this.jakuziDataGridViewCheckBoxColumn.Width = 70;
//
// numberOfWindowsDataGridViewTextBoxColumn
//
this.numberOfWindowsDataGridViewTextBoxColumn.DataPropertyName =NumberOfWindows;
this.numberOfWindowsDataGridViewTextBoxColumn.HeaderText =מספרחלונות;
this.numberOfWindowsDataGridViewTextBoxColumn.Name =numberOfWindowsDataGridViewTextBoxColumn;
//
// airConditionedDataGridViewCheckBoxColumn
//
this.airConditionedDataGridViewCheckBoxColumn.DataPropertyName =AirConditioned;
this.airConditionedDataGridViewCheckBoxColumn.HeaderText =מיזוגאוויר;
this.airConditionedDataGridViewCheckBoxColumn.Name =airConditionedDataGridViewCheckBoxColumn;
this.airConditionedDataGridViewCheckBoxColumn.Width = 80;
//
// cleaningDateDataGridViewTextBoxColumn
//
this.cleaningDateDataGridViewTextBoxColumn.DataPropertyName =CleaningDate;
this.cleaningDateDataGridViewTextBoxColumn.HeaderText =תאריךניקיון;
this.cleaningDateDataGridViewTextBoxColumn.Name =cleaningDateDataGridViewTextBoxColumn;
//
// landScapeDataGridViewTextBoxColumn
//
this.landScapeDataGridViewTextBoxColumn.DataPropertyName =LandScape;
this.landScapeDataGridViewTextBoxColumn.HeaderText =איכותנוף;
this.landScapeDataGridViewTextBoxColumn.Name =landScapeDataGridViewTextBoxColumn;
this.landScapeDataGridViewTextBoxColumn.Width = 90;
//
// ChooseGroupRoomsDialog
//
this.AcceptButton = this.okButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(1190,399);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.okButton);
this.Controls.Add(this.table);
this.Name =ChooseGroupRoomsDialog;
this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.RightToLeftLayout = true;
this.Text =בחרחדרים;
((System.ComponentModel.ISupportInitialize)(this.table))。EndInit();
((System.ComponentModel.ISupportInitialize)(this.roomBindingSource))。EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.BindingSource roomBindingSource;
private System.Windows.Forms.DataGridView表;
private System.Windows.Forms.Button okButton;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.DataGridViewCheckBoxColumn chooseColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn idDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn numberOfBedsDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn typeOfRoomStringDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewCheckBoxColumn viewingSeaDataGridViewCheckBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn costDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewCheckBoxColumn jakuziDataGridViewCheckBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn numberOfWindowsDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewCheckBoxColumn airConditionedDataGridViewCheckBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn cleaningDateDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn landScapeDataGridViewTextBoxColumn;
}
}


解决方案

奇怪的是,我无法确定。当我在您的 //不工作!! 线上设置一个断点时,传入一个人工列表中的五个我正在填充的两个房间,手表窗口第一次显示 table [index,chooseColumn.Index] 作为 DataGridViewTextBoxCell 的类型,一个 DataGridViewCheckBoxCell 第二,我甚至不知道如何可能。



编辑:我只是想出了如何可能。您有列和行索引向后。您的非工作行应为:

 表[chooseColumn.Index,index] .Value = true; //加工!! 

再次编辑:好的,我以前的猜想和链接到这个答案也是必要的。您必须修复索引顺序,并在DataBindingComplete处理程序中将单元格值设置为true:

 列表<房间> _值; 
public List< Room>价值
{
get
{
列表<房间> retList = new List< Room>(); (int i = 0; i
{
DataGridViewCheckBoxCell cell =(DataGridViewCheckBoxCell)table [chooseColumn.Index,i];
if(cell.Value!= null&&(bool)cell.Value)
retList.Add((Room)table.Rows [i] .DataBoundItem);
}
return retList;
}
set
{
_value = value;
}
}

private void table_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
if(_value == null)return;
列表<房间> data =(List< Room>)(table.DataSource);
foreach(_value中的房间)
{
int index = data.FindIndex(roomA => roomA.Id == room.Id);
if(index!= -1)
{
table [chooseColumn.Index,index] .Value = true; //加工!!
}
}
}


I have a problem. My code contains DataGridView and UnBound CheckBoxColumn. My dialog should be for choose rooms from list. For second time you want to see what are you chose in first time.

Code:

ChooseGroupRoomDialog.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BE; // My namespace
using ListExtensions; // My namespace

namespace PLForms
{
    public partial class ChooseGroupRoomsDialog : Form
    {
        public List<Room> Value
        {
            get
            {
                List<Room> retList = new List<Room>();
                for (int i = 0; i < table.RowCount; i++)
                {
                    DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)table[chooseColumnIndex, i];
                    if (cell.Value != null && (bool)cell.Value)
                        retList.Add((Room)table.Rows[i].DataBoundItem);
                }
                return retList;
            }
            set
            {
                List<Room> data = (List<Room>)(table.DataSource);
                foreach (Room room in value)
                {
                    int index = data.FindIndex(roomA => roomA.Id == room.Id);
                    if (index != -1)
                    {
                        table[index, chooseColumnIndex].Value = true; //Not working!!
                    }
                }
            }
        }
        private int chooseColumnIndex;
        public ChooseGroupRoomsDialog(List<Room> dataSource)
        {
            InitializeComponent();
            table.DataSource = dataSource.ToList();
            DialogResult = DialogResult.Cancel;
            foreach (DataGridViewColumn col in table.Columns)
                col.ReadOnly = true;
            chooseColumnIndex = chooseColumn.Index;
            chooseColumn.ReadOnly = false;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

ChooseGroupRoomDialog.Designer.cs

namespace PLForms
{
    partial class ChooseGroupRoomsDialog
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            this.table = new System.Windows.Forms.DataGridView();
            this.roomBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.okButton = new System.Windows.Forms.Button();
            this.cancelButton = new System.Windows.Forms.Button();
            this.chooseColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.numberOfBedsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.typeOfRoomStringDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.viewingSeaDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.costDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.jakuziDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.numberOfWindowsDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.airConditionedDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.cleaningDateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.landScapeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.table)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.roomBindingSource)).BeginInit();
            this.SuspendLayout();
            // 
            // table
            // 
            this.table.AllowUserToAddRows = false;
            this.table.AllowUserToDeleteRows = false;
            this.table.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.table.AutoGenerateColumns = false;
            this.table.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.table.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.chooseColumn,
            this.idDataGridViewTextBoxColumn,
            this.numberOfBedsDataGridViewTextBoxColumn,
            this.typeOfRoomStringDataGridViewTextBoxColumn,
            this.viewingSeaDataGridViewCheckBoxColumn,
            this.costDataGridViewTextBoxColumn,
            this.jakuziDataGridViewCheckBoxColumn,
            this.numberOfWindowsDataGridViewTextBoxColumn,
            this.airConditionedDataGridViewCheckBoxColumn,
            this.cleaningDateDataGridViewTextBoxColumn,
            this.landScapeDataGridViewTextBoxColumn});
            this.table.DataSource = this.roomBindingSource;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.GradientInactiveCaption;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.ControlText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            this.table.DefaultCellStyle = dataGridViewCellStyle1;
            this.table.Location = new System.Drawing.Point(129, 12);
            this.table.MultiSelect = false;
            this.table.Name = "table";
            this.table.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.table.Size = new System.Drawing.Size(1047, 372);
            this.table.TabIndex = 1;
            // 
            // roomBindingSource
            // 
            this.roomBindingSource.DataSource = typeof(BE.Room);
            // 
            // okButton
            // 
            this.okButton.Location = new System.Drawing.Point(12, 12);
            this.okButton.Name = "okButton";
            this.okButton.Size = new System.Drawing.Size(111, 44);
            this.okButton.TabIndex = 2;
            this.okButton.Text = "אישור";
            this.okButton.UseVisualStyleBackColor = true;
            this.okButton.Click += new System.EventHandler(this.okButton_Click);
            // 
            // cancelButton
            // 
            this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.cancelButton.Location = new System.Drawing.Point(12, 62);
            this.cancelButton.Name = "cancelButton";
            this.cancelButton.Size = new System.Drawing.Size(111, 44);
            this.cancelButton.TabIndex = 2;
            this.cancelButton.Text = "ביטול";
            this.cancelButton.UseVisualStyleBackColor = true;
            this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
            // 
            // chooseColumn
            // 
            this.chooseColumn.HeaderText = "בחירה";
            this.chooseColumn.Name = "chooseColumn";
            this.chooseColumn.Width = 70;
            // 
            // idDataGridViewTextBoxColumn
            // 
            this.idDataGridViewTextBoxColumn.DataPropertyName = "Id";
            this.idDataGridViewTextBoxColumn.HeaderText = "מספר זיהוי";
            this.idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn";
            // 
            // numberOfBedsDataGridViewTextBoxColumn
            // 
            this.numberOfBedsDataGridViewTextBoxColumn.DataPropertyName = "NumberOfBeds";
            this.numberOfBedsDataGridViewTextBoxColumn.HeaderText = "מספר מיטות";
            this.numberOfBedsDataGridViewTextBoxColumn.Name = "numberOfBedsDataGridViewTextBoxColumn";
            // 
            // typeOfRoomStringDataGridViewTextBoxColumn
            // 
            this.typeOfRoomStringDataGridViewTextBoxColumn.DataPropertyName = "TypeOfRoomString";
            this.typeOfRoomStringDataGridViewTextBoxColumn.HeaderText = "סוג חדר";
            this.typeOfRoomStringDataGridViewTextBoxColumn.Name = "typeOfRoomStringDataGridViewTextBoxColumn";
            this.typeOfRoomStringDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // viewingSeaDataGridViewCheckBoxColumn
            // 
            this.viewingSeaDataGridViewCheckBoxColumn.DataPropertyName = "ViewingSea";
            this.viewingSeaDataGridViewCheckBoxColumn.HeaderText = "נוף לים";
            this.viewingSeaDataGridViewCheckBoxColumn.Name = "viewingSeaDataGridViewCheckBoxColumn";
            this.viewingSeaDataGridViewCheckBoxColumn.Width = 70;
            // 
            // costDataGridViewTextBoxColumn
            // 
            this.costDataGridViewTextBoxColumn.DataPropertyName = "Cost";
            this.costDataGridViewTextBoxColumn.HeaderText = "מחיר";
            this.costDataGridViewTextBoxColumn.Name = "costDataGridViewTextBoxColumn";
            this.costDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // jakuziDataGridViewCheckBoxColumn
            // 
            this.jakuziDataGridViewCheckBoxColumn.DataPropertyName = "Jakuzi";
            this.jakuziDataGridViewCheckBoxColumn.HeaderText = "ג\'קוזי";
            this.jakuziDataGridViewCheckBoxColumn.Name = "jakuziDataGridViewCheckBoxColumn";
            this.jakuziDataGridViewCheckBoxColumn.Width = 70;
            // 
            // numberOfWindowsDataGridViewTextBoxColumn
            // 
            this.numberOfWindowsDataGridViewTextBoxColumn.DataPropertyName = "NumberOfWindows";
            this.numberOfWindowsDataGridViewTextBoxColumn.HeaderText = "מספר חלונות";
            this.numberOfWindowsDataGridViewTextBoxColumn.Name = "numberOfWindowsDataGridViewTextBoxColumn";
            // 
            // airConditionedDataGridViewCheckBoxColumn
            // 
            this.airConditionedDataGridViewCheckBoxColumn.DataPropertyName = "AirConditioned";
            this.airConditionedDataGridViewCheckBoxColumn.HeaderText = "מיזוג אוויר";
            this.airConditionedDataGridViewCheckBoxColumn.Name = "airConditionedDataGridViewCheckBoxColumn";
            this.airConditionedDataGridViewCheckBoxColumn.Width = 80;
            // 
            // cleaningDateDataGridViewTextBoxColumn
            // 
            this.cleaningDateDataGridViewTextBoxColumn.DataPropertyName = "CleaningDate";
            this.cleaningDateDataGridViewTextBoxColumn.HeaderText = "תאריך ניקיון";
            this.cleaningDateDataGridViewTextBoxColumn.Name = "cleaningDateDataGridViewTextBoxColumn";
            // 
            // landScapeDataGridViewTextBoxColumn
            // 
            this.landScapeDataGridViewTextBoxColumn.DataPropertyName = "LandScape";
            this.landScapeDataGridViewTextBoxColumn.HeaderText = "איכות נוף";
            this.landScapeDataGridViewTextBoxColumn.Name = "landScapeDataGridViewTextBoxColumn";
            this.landScapeDataGridViewTextBoxColumn.Width = 90;
            // 
            // ChooseGroupRoomsDialog
            // 
            this.AcceptButton = this.okButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.cancelButton;
            this.ClientSize = new System.Drawing.Size(1190, 399);
            this.Controls.Add(this.cancelButton);
            this.Controls.Add(this.okButton);
            this.Controls.Add(this.table);
            this.Name = "ChooseGroupRoomsDialog";
            this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.RightToLeftLayout = true;
            this.Text = "בחר חדרים";
            ((System.ComponentModel.ISupportInitialize)(this.table)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.roomBindingSource)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.BindingSource roomBindingSource;
        private System.Windows.Forms.DataGridView table;
        private System.Windows.Forms.Button okButton;
        private System.Windows.Forms.Button cancelButton;
        private System.Windows.Forms.DataGridViewCheckBoxColumn chooseColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn idDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn numberOfBedsDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn typeOfRoomStringDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewCheckBoxColumn viewingSeaDataGridViewCheckBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn costDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewCheckBoxColumn jakuziDataGridViewCheckBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn numberOfWindowsDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewCheckBoxColumn airConditionedDataGridViewCheckBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn cleaningDateDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn landScapeDataGridViewTextBoxColumn;
    }
}

解决方案

Something weird is happening that I can't pin down. When I set a break point on your //Not working!! line, passing in an artificial list of two rooms out of five that I'm populating with, the watch window shows the type of table[index, chooseColumn.Index] as a DataGridViewTextBoxCell the first time and a DataGridViewCheckBoxCell the second, and I'm not even sure how that's possible.

EDIT: I just figured out how it's possible. You have the column and row indices backward. Your non-working line should be:

table[chooseColumn.Index, index].Value = true; //Working!!

EDIT AGAIN: OK, my previous conjectures and link to this answer were also necessary. You have to fix the index order AND set the cell value to true in the DataBindingComplete handler:

    List<Room> _value;
    public List<Room> Value
    {
        get
        {
            List<Room> retList = new List<Room>();
            for (int i = 0; i < table.RowCount; i++)
            {
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)table[chooseColumn.Index, i];
                if (cell.Value != null && (bool)cell.Value)
                    retList.Add((Room)table.Rows[i].DataBoundItem);
            }
            return retList;
        }
        set
        {
            _value = value;
        }
    }

    private void table_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        if (_value == null) return;
        List<Room> data = (List<Room>)(table.DataSource);
        foreach (Room room in _value)
        {
            int index = data.FindIndex(roomA => roomA.Id == room.Id);
            if (index != -1)
            {
                table[chooseColumn.Index, index].Value = true; //Working!!
            }
        }
    }

这篇关于未绑定DataGridViewCheckBoxCell设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆