I have an UltraGrid with checkboxes columns.
There are initialized from a web method in Java which is calling the DB for the data. Anyway, when I run the application some checkboxes are already filled and I can check or uncheck. I would like to retrieve the columns which have been updated. Currently, I did something which allows me to retrieve the status of all the checkboxes and not only the ones I've modified.
Here's the code, the method GetStatusForRow retrieve the status of the checkboxes and GetStatusCheckboxes call the previous method to get the status of all the checkboxes.
public partial class FrmCVault : Form
{
#region Properties
/// <summary>
/// Base filter lists
/// </summary>
//TODO: Change to a CVaultConfiguration Class
public List<BASEFILTER> lBaseFilters { get; private set; }
public List<SITE> lSites { get; private set; }
public List<CONFIGURE> lConfigures{ get; private set; }
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public FrmCVault()
{
InitializeComponent();
this.lBaseFilters = null;
this.lSites = null;
this.lConfigures = null;
}
#endregion
#region Private methods
#region Compare byte[]
/// <summary>
/// Compare two byte[]
/// </summary>
/// <param name="b1"></param>
/// <param name="b2"></param>
/// <returns></returns>
private bool Compare(byte[] b1, byte[] b2)
{
// return Encoding.ASCII.GetString(b1) == Encoding.ASCII.GetString(b2);
return b1.SequenceEqual(b2);
}
#endregion
#region Fill the table with Basefilter data
/// <summary>
/// Fill the table with Basefilter data
/// </summary>
private void retrieveData()
{
EVPTProviderBase provider = DataRepository.EVPTProvider;
ObjectDataSet ds = provider.GetBaseFilter();
if (ds != null)
{
this.lBaseFilters = ds.arBASEFILTER.ToList();
this.lSites = ds.arSITE.ToList();
this.lConfigures = ds.arCONFIGURE.ToList();
}
}
#endregion
#region Load Base filter data from DB
/// <summary>
/// Load data from the database
/// </summary>
private void LoadDataSource()
{
CVaultDataSource.Rows.Clear();
if (this.lBaseFilters != null)
{
var filters = from filterBase in this.lBaseFilters
orderby filterBase.EVPTCODE
select new { Id = filterBase.ID, cvault = filterBase.CVAULTCODE, evpt = filterBase.EVPTCODE, desc = filterBase.EVPTDESIGNATION, duration = filterBase.DURATION, time = filterBase.ETDTIME };
var sitesDB = from sites in this.lSites
orderby sites.KEY
select new { SiteKey = sites.KEY, SiteId = sites.ID };
var configs = from config in this.lConfigures
select new { site = config.SITE_ID, baseF = config.BASEFILTER_ID };
object[][] table = new object[filters.Count()][];
int i = 0;
foreach (var item in filters)
{
int j = 0;
table[i] = new object[5 + sitesDB.Count()];
table[i][j++] = item.cvault;
table[i][j++] = item.evpt;
table[i][j++] = item.desc;
table[i][j++] = item.duration;
table[i][j++] = item.time;
foreach (var site in sitesDB)
{
table[i][j] = false;
foreach (var conf in configs)
{
if (Compare(site.SiteId, conf.site) && Compare(conf.baseF, item.Id))
{
table[i][j] = true;
break;
}
}
j++;
}
i++;
}
foreach (var item in table)
{
CVaultDataSource.Rows.Add(item);
}
}
}
#endregion
#region Add column sites
/// <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 =>
{
if (siteCol != "OTHER")
{
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;
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);
}
}
#endregion
#region Retrieve status of checkboxes
/// <summary>
/// Retrieve the checkboxes's status of the last row selected
/// </summary>
/// <param name="b"></param>
/// <param name="row"></param>
/// <returns></returns>
private Dictionary<string, bool> GetStatusForRow(UltraDataBand b, UltraDataRow 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.GetCellValue(col)));
}
return statusChecked;
}
#endregion
#region Retrieve status of hours columns
private Dictionary<string, string> GetStatusHoursForRow(UltraDataBand b, UltraGridRow row)
{
Dictionary<string, string> statusChecked = new Dictionary<string, string>();
foreach (UltraDataColumn col in b.Columns.Cast<UltraDataColumn>()
.Where(x => x.Tag != null &&
x.Tag.ToString() == "SITE_COL"))
{
//statusChecked.Add(,);
}
return statusChecked;
}
#endregion
#endregion
#region Form Events
/// <summary>
/// Form loading event
/// </summary>
/// <param name="sender">Form</param>
/// <param name="e">Arguments (empty)</param>
private void FrmCVault_Load(object sender, EventArgs e)
{
this.retrieveData();
this.AddColumnSites();
this.LoadDataSource();
CVaultGrid.DataBind();
}
private void ultraButton2_Click(object sender, EventArgs e)
{
CVaultGrid.ActiveRow.Update();
CVaultGrid.DisplayLayout.Bands[0].AddNew();
}
private void FrmCVault_Shown(object sender, EventArgs e)
{
CVaultGrid.DisplayLayout.Bands[0].AddNew();
}
private void vl_ItemNotInList(object sender, ValidationErrorEventArgs e)
{
var ultrCombo = sender as UltraComboEditor;
e.RetainFocus = true;
if (e.LastValidValue != null)
ultrCombo.Value = e.LastValidValue;
else if (ultrCombo.Items.Count>0)
ultrCombo.SelectedItem = ultrCombo.Items[0];
}
private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
CVaultGrid.UpdateData();
}
}
private void Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void SAVE_Click(object sender, System.EventArgs e)
{
this.GetStatusCheckboxes();
this.GetStatusHours();
}
/// <summary>
/// Get the status of checkboxes which have been changed (check or uncheck)
/// </summary>
private void GetStatusCheckboxes()
{
var statusChecked = new Dictionary<string, Dictionary<string, bool>>();
foreach (UltraDataRow row in CVaultDataSource.Rows)
{
statusChecked.Add(row.GetCellValue(0).ToString(), GetStatusForRow(CVaultDataSource.Band, row));
}
foreach (KeyValuePair<string, Dictionary<string, bool>> kvp in statusChecked)
{
foreach (var sr in kvp.Value)
{
Console.WriteLine(string.Format("[{0}] Status site: {1} is {2}", kvp.Key, sr.Key, sr.Value));
}
}
Console.WriteLine("\r\n");
}
/// <summary>
/// Get back the hour which have been updated
/// From the column DURATION or ETD
/// </summary>
private void GetStatusHours()
{
//GetStatusHoursForRow(CVaultDataSource.Band, CVaultGrid.ActiveRow);
//Dictionary<string, bool> 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");
}
#endregion
private void CVaultGrid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
}
}
Here's a screen to see the windows:
Aucun commentaire:
Enregistrer un commentaire