如何获取复选框的状态

时间:2015-10-01 09:53:23

标签: c# .net winforms checkbox infragistics

当我选中或取消选中时,我想知道我的chekcbox的状态。

我正在根据我的数据库创建复选框列(我的表中一行有一列)。我正在使用Infragistics 2015。 所以这是我创建列的代码:

/// <summary>
/// Add columns in table according to the sites stored in DB
/// </summary>
/// <remarks>The action is used to create site column</remarks>
private void AddColumnSites()
{
    const string siteCol = "SITE_COL";

    var addNewSite = new Action<string>(site =>
    {
        var ultraGridBand = this.CVaultGrid.DisplayLayout.Bands[0];

        var gridDataColumn = new UltraDataColumn(site);
        gridDataColumn.DataType = typeof(bool);
        gridDataColumn.Tag = siteCol;
        gridDataColumn.DataType = typeof(bool);
        gridDataColumn.DefaultValue = false;
        gridDataColumn.SubObjectPropChanged += this.OnSubObjectPropChanged;
        this.CVaultDataSource.Band.Columns.AddRange(new object[] {
            gridDataColumn
        });
    });

    for (int i = this.CVaultDataSource.Band.Columns.Count-1; i >= 0 ; i--)
    {
        if (this.CVaultDataSource.Band.Columns[i].Tag == siteCol)
        {
            this.CVaultDataSource.Band.Columns.RemoveAt(i);
        }
    }

    var sitesDB = from sites in this.lSites
                  orderby sites.KEY
                  select  sites.KEY ;

    foreach (var item in sitesDB)
    {
        addNewSite(item);
    }
}

我想创建一个私有方法来获取复选框的状态,并以字符串或布尔值返回结果。

这是一个屏幕: Example

2 个答案:

答案 0 :(得分:1)

您可以编写一个方法,将每个复选框值存储在Dictionary<string,bool>中,其中字符串键是站点名称,值是根据复选框标记的状态true / false。您可以将您感兴趣的UltraGridBand和UltraGridRow传递给此方法以获取其值

private Dictionary<string, bool> GetStatusForRow(UltraGridBand b, 
                                                 UltraGridRow row)
{
    Dictionary<string, bool> statusChecked = new Dictionary<string, bool>();
    foreach (UltraGridColumn col in b.Columns.Cast<UltraGridColumn>()
                                     .Where(x => x.Tag != null && 
                                            x.Tag.ToString() == "SITE_COL"))
    {
        statusChecked.Add(col.Key, Convert.ToBoolean(row.Cells[col].Value));
    }
    return statusChecked;
}

并用这样的东西读回来

....
Dictionary<string, bool> statusChecked = GetStatusForRow(CVaultGrid.DisplayLayout.Bands[0], 
                                                         CVaultGrid.ActiveRow);
foreach(KeyValuePair kvp in statusChecked)
   Console.WriteLine("Status site:" + kvp.Key + " is " + kvp.Value.ToString());

修改
为了更好地调试此代码,您可以使用更传统的

替换GetStatusRow中的foreach循环
foreach (UltraGridColumn col in b.Columns)
{
    if(col.Tag != null && col.Tag.ToString() == "SITE_COL")
         statusChecked.Add(col.Key, Convert.ToBoolean(row.Cells[col].Value));
}

编辑2
要遍历所有行并检索每行的复选框值,您需要这样的

private void GetStatusCheckboxes()
{
    foreach (UltraGridRow row in CVaultGrid.Rows)
    {
        Dictionary<string, bool> statusChecked;
        statusChecked = GetStatusForRow(CVaultDataSource.Band, row);

        // Here you should replace the Console.WriteLine with the code 
        // that uses the status of the current indexed row by the foreach
        foreach (KeyValuePair<string, bool> kvp in statusChecked)
            Console.WriteLine("RowIndex=" + row.Index + ", " + 
                              "Status site:" + kvp.Key + " is " + kvp.Value.ToString());
        Console.WriteLine("\r\n");
   }
}

答案 1 :(得分:0)

@Steve 我只是改变了类型,它的工作完美! 你写的正是我所需要的! :d

以下是代码:

private Dictionary<string, bool> GetStatusForRow(UltraDataBand b,
                                                 UltraGridRow row)
        {

            Dictionary<string, bool> statusChecked = new Dictionary<string, bool>();
            foreach (UltraDataColumn col in b.Columns.Cast<UltraDataColumn>()
                                             .Where(x => x.Tag != null &&
                                                    x.Tag.ToString() == "SITE_COL"))
            {

                statusChecked.Add(col.Key, Convert.ToBoolean(row.Cells[col.Key].Value));

            }
            return statusChecked;
        }

电话:

private void SaveCvaultData()
        {
            GetStatusForRow(CVaultDataSource.Band, CVaultGrid.ActiveRow);
            Dictionary<string, bool> statusChecked = GetStatusForRow(CVaultDataSource.Band, CVaultGrid.ActiveRow);
            foreach (KeyValuePair<string, bool> kvp in statusChecked)
                MessageBox.Show("Status site:" + kvp.Key + " is " + kvp.Value.ToString());
        }

谢谢史蒂夫!

编辑:尝试检索所有状态。 这就是我所做的,它不起作用:

private void GetStatusCheckboxes()
        {
            Dictionary<string, bool> statusChecked = GetStatusForRow(CVaultDataSource.Band, CVaultGrid.ActiveRow);
            foreach (UltraGridRow row in CVaultGrid.Rows)
            {
                statusChecked = GetStatusForRow(CVaultDataSource.Band, CVaultGrid.ActiveRow);
            }

            foreach (KeyValuePair<string, bool> kvp in statusChecked)
                Console.WriteLine("Status site:" + kvp.Key + " is " + kvp.Value.ToString());
            Console.WriteLine("\r\n");
        }