gpt4 book ai didi

c# - 如何获取复选框的状态

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:17 26 4
gpt4 key购买 nike

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

我正在根据我的数据库创建复选框列(我的表中一行对应一列)。我正在使用 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);
}
}

我想创建一个私有(private)方法来获取复选框的状态并以字符串或 bool 值的形式返回结果。

这是一个屏幕: Example

最佳答案

您可以编写一个方法,将每个复选框的值存储在 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");
}
}

关于c# - 如何获取复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32884565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com