RepositoryItemCheckEdit不会保持检查状态

时间:2013-04-22 14:57:22

标签: winforms devexpress devexpress-windows-ui

我尝试使用RepositoryItemCheckEditGridViewdevexpress添加Winforms。但是,我只能选中一个复选框。如果我检查另一个,我之前检查的复选框将取消选中。我按照我在网上找到的所有内容,但无法完成这项工作。我错过了什么?

代码部分我插入了列:

gcIsEmirleri.DataSource = (from i in isemirleri
                            select new
                            {
                                ID = i.isEmriId,
                                // other attributes
                            }).ToList();

GridColumn column = gvIsEmirleri.Columns["Sec"];
if (column == null)
{
    gvIsEmirleri.BeginUpdate();

    DataColumn col = new DataColumn("Sec", typeof(bool));
    column = gvIsEmirleri.Columns.AddVisible("Sec");
    col.VisibleIndex = 0;
    col.Caption = "Sec";
    col.Name = "Sec";
    col.OptionsColumn.AllowEdit = true;

    gvIsEmirleri.EndUpdate();


    gvIsEmirleri.Columns["Sec"].UnboundType = DevExpress.Data.UnboundColumnType.Boolean;

    RepositoryItemCheckEdit chk = new RepositoryItemCheckEdit();
    chk.ValueChecked = true;
    chk.ValueUnchecked = false;
    gvIsEmirleri.Columns["Sec"].ColumnEdit = chk;
    chk.QueryCheckStateByValue += chk_QueryCheckStateByValue;
}

代码部分我将复选框设为两个而不是三个:

private void chk_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
{
    if (e.Value == null)
    {
        e.CheckState = CheckState.Unchecked;
        e.Handled = true;
    }
}

编辑:我创建了List<bool> chkList;并执行以下操作: 此功能已添加到检查项“CheckStateChanged

private void chk_CheckStateChanged(object sender, EventArgs e)
{
    CheckEdit chk = sender as CheckEdit;
    if (chk.Checked) 
        chkList[gvIsEmirleri.FocusedRowHandle] = true;
    else 
        chkList[gvIsEmirleri.FocusedRowHandle] = false;

    FillBindingSource();
}

FillBindingSource中我添加了以下几行:

for (int i = 0; i < chkList.Count; i++)
{
    if (chkList[i])
        gvIsEmirleri.SetRowCellValue(i, "Sec", true);
}

我调试了这些行,我发现List具有正确的bool值,并且必须时会运行gvIsEmirleri.SetRowCellValue(i, "Sec", true);。但是,它仍然无效。

1 个答案:

答案 0 :(得分:3)

我的猜测是:您正在使用未绑定的列,并且您没有保存已选中/未确认的信息,因此,在离开所选行后,checkBox会获得它的初始值(未标记)。

为此,我建议您处理视图的CustomUnboundColumnData事件。这是一个简单的:

readonly Dictionary<object, bool> checkedMap = new Dictionary<object, bool>();

private void viewScales_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
     // Check what column
     if (e.Column != gvIsEmirleri.Columns["Sec"])
         return;

     if (e.IsGetData)
     {
          // check if the row has been checked and set it's value using e.Value
          bool checked;
          if (checkedMap.TryGetValue(e.Row, out checked))
              e.Value = checked;
     }

     if (e.IsSetData)
     {
          var checked = Convert.ToBoolean(e.Value);

          // Check if the key already exist
          if (checkedMap.ContainsKey(e.Row))
                scaleMap.Remove(e.Row);

          checkedMap.Add(e.Row, checked);
    }
 }

注意:这是我解决类似问题的方法,但我没有测试我刚写的代码。