Reputation: 938
I want to know the state of my chekcboxes when I check or uncheck them.
I'm creating checkboxes columns according to my DB (one column for one line in my table). I'm using Infragistics 2015. So here's my code to create the columns :
/// <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);
}
}
I would like to create a private method to get the state of the checkboxes and returns me the result in a string or a boolean.
Upvotes: 2
Views: 283
Reputation: 216293
You could write a method that stores each checkbox value in a Dictionary<string,bool>
where the string key is the site name and the value is the status true/false according to the checkbox mark. You could pass to this method the UltraGridBand and the UltraGridRow that you are interested to get its values back
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;
}
and read it back with something like this
....
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());
EDIT
To better debug this code you could replace the foreach loop inside GetStatusRow with a more traditional
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));
}
EDIT 2
To loop over all rows and retrieve the values of the checkbox for every row you need something like this
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");
}
}
Upvotes: 1
Reputation: 938
@Steve I just change the type and it's working perfectly ! What you wrote was exactly what I needed ! :D
Here's the code:
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;
}
And the call:
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());
}
Thank you Steve !
EDIT : Try to retrieve all status. This is what I did and it's not working :
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");
}
Upvotes: 0