如何获取复选框的状态
How to get the state of checkboxes
我想知道选中或取消选中时我的 chekcbox 的状态。
我正在根据我的数据库创建复选框列(在我的 table 中一行一行)。我正在使用 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);
}
}
我想创建一个私有方法来获取复选框的状态,然后 returns 将结果以字符串或布尔值的形式告诉我。
这是一个屏幕:
您可以编写一个方法,将每个复选框值存储在 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");
}
}
@史蒂夫
我只是改变了类型,它工作得很好!
你写的正是我需要的! :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");
}
我想知道选中或取消选中时我的 chekcbox 的状态。
我正在根据我的数据库创建复选框列(在我的 table 中一行一行)。我正在使用 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);
}
}
我想创建一个私有方法来获取复选框的状态,然后 returns 将结果以字符串或布尔值的形式告诉我。
这是一个屏幕:
您可以编写一个方法,将每个复选框值存储在 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");
}
}
@史蒂夫 我只是改变了类型,它工作得很好! 你写的正是我需要的! :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");
}