Question

I try to add a RepositoryItemCheckEdit to my GridView using devexpress and Winforms. However, I can get only one checkbox be checked. If I check another one, the checkbox I checked before becomes unchecked. I followed everything I can find on the net, but couldn't make this work. What am I missing?

The code part I insert the column:

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;
}

The code part I make the checkbox two-stated instead of three:

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

EDIT: I created a List<bool> chkList; and do the following operations: This function is added to checkedits' 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();
}

In FillBindingSource I added the lines:

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

I debug these lines, I see that List has correct bool values and gvIsEmirleri.SetRowCellValue(i, "Sec", true); is operated when it has to. However, it still doesn't work.

Était-ce utile?

La solution

My guess is : You are using an unbound Column, and you are not saving the checked / unckecked info, so, after the selected row is left, the checkBox get it's initial value (unckecked).

For this, I suggest you handle the CustomUnboundColumnData event of your view. Here is a simple :

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);
    }
 }

Note : This is the way I resolved a similar problem, but I did not test the code I just wrote.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top