Hi,
I want to prohibit the user to enter anything else than a number containing 3 digits in one cell of any RowAdded or any edited row. I use CellValueChanging with
eventArgs._cancel = true; but the user can entera value in the other column and BatchUpdate a value of 0 in the database for the cell I want formatted "000" (varchar(11) in SQL Server). I want only to allow a value from 1 to 900 with the format "000" to go in the database for the column key "code", both on the parent and the child rows. If the user enter a bad value, I show a panel containing a message, using style="display:block;". Here is the related code and markup:
<!-- CLIENT SIDE -->
<ig:WebHierarchicalDataGrid ID="whdg" runat="server" AutoGenerateBands="False" AutoGenerateColumns="False" DataKeyFields="id" Height="600px" InitialDataBindDepth="-1" Key="WBS_Niv1" Width="600px" StyleSetName="Default">
<ClientEvents Click="whdg_ContainerGrid_Click" />
<Columns>
<ig:BoundDataField DataFieldName="id" Key="id" Hidden="True" />
<ig:BoundDataField CssClass="Cell_Left_Niv1" DataFieldName="code" Key="code" Width="50px" DataFormatString="{0:d2}">
<Header CssClass="Entete_Left" Text="Code" />
</ig:BoundDataField>
<ig:BoundDataField CssClass="Cell_Left_Niv1" DataFieldName="nom" Key="nom" Width="300px">
<Header CssClass="Entete_Left" Text="SDP" />
<ig:TemplateDataField CssClass="Cell_Center_Niv1" Key="Add_D" Width="20px" Hidden="True">
<Header CssClass="Entete_Center" Text="" />
<ItemTemplate>
<asp:Image ID="imgAdd" ToolTip="Ajouter" runat="server" ImageUrl="img/Ajouter.png" CssClass="imgClickYes" />
</ItemTemplate>
</ig:TemplateDataField>
<ig:TemplateDataField CssClass="Cell_Center_Niv1" Key="Edit_DG" Width="20px" Hidden="True">
<asp:Image ID="imgEdit" ToolTip="Modifier" runat="server" ImageUrl="img/Modifier.png" CssClass="imgClickYes" />
<ig:TemplateDataField CssClass="Cell_Center_Niv1" Key="Delete_DG" Width="20px">
<asp:Image ID="imgDelete" ToolTip="Supprimer" runat="server" ImageUrl="img/Supprimer.png" CssClass="imgClickYes" />
<ig:TemplateDataField Hidden="True" Key="HasHours">
</Columns>
<Behaviors>
<ig:Selection CellClickAction="Cell" Enabled="true" />
<ig:Activation>
<ActivationClientEvents />
</ig:Activation>
<ig:RowSelectors>
</ig:RowSelectors>
<ig:EditingCore Enabled="true" EnableInheritance="true" AutoCRUD="false" BatchUpdating="True" EditingClientEvents-CellValueChanging="whdg_CellValueChanging">
<EditingClientEvents CellValueChanging="whdg_CellValueChanging"></EditingClientEvents>
<ig:RowAdding AddNewRowCssClass="igg_AddedRow" EditCellCssClass="igg_AddedRow" EnableInheritance="True">
<EditModeActions EnableOnKeyPress="True" MouseClick="Single" />
</ig:RowAdding>
<ig:RowDeleting ButtonHtml="Delete" ShowDeleteButton="false">
</ig:RowDeleting>
<ig:CellEditing Enabled="true" EditModeActions-EnableF2="true" EditCellCssClass="igg_AddedRow" EnableInheritance="True">
<ColumnSettings>
<ig:EditingColumnSetting ColumnKey="code" EditorID="whdg_NumericEditorProvider1" />
<ig:EditingColumnSetting ColumnKey="nom" />
</ColumnSettings>
<EditModeActions EnableOnKeyPress="True" MouseClick="Double" />
</ig:CellEditing>
</Behaviors>
</ig:EditingCore>
<Bands>
<ig:Band AutoGenerateColumns="false" DataKeyFields="id" DataMember="WBS_Niv2" Key="WBS_Niv2" ShowHeader="false">
<ig:BoundDataField DataFieldName="ref_WBS_Niv1_ID" Key="ref_WBS_Niv1_ID" Hidden="true" />
<ig:BoundDataField CssClass="Cell_Left" DataFieldName="code" Key="code" Width="50" DataFormatString="{0:d2}">
<ig:BoundDataField CssClass="Cell_Left" DataFieldName="nom" Key="nom" Width="317px">
<ig:TemplateDataField CssClass="Cell_Center" Key="Edit_D" Width="20px" Hidden="True" >
<ig:TemplateDataField CssClass="Cell_Center" Key="Delete_D" Width="20px" >
<ig:CellEditing EditCellCssClass="igg_AddedRow" EnableInheritance="True">
<ig:EditingColumnSetting ColumnKey="code" />
<EditModeActions EnableOnKeyPress="True" />
</ig:Band>
</Bands>
</ig:WebHierarchicalDataGrid>
<div>
<center>
<asp:Panel ID="pnlBoxMessage" runat="server" CssClass="pnlBoxMsg" style="display:block;">
<table style="width: 100%;">
<tr>
<td style="height: 52px; text-align: center;">
<asp:Label ID="lblboxMessage" runat="server" Text="Message" CssClass="lblBoxMsgGreen" />
</td>
</tr>
</table>
<table style="width: 100%">
<td style="text-align: center;">
<input id="btnBoxClose" type="button" value="Fermer" class="myButton" height="33px" style="width: 91px;" onclick="document.getElementById('MainContent_pnlBoxMessage').style.display = 'none';" />
</asp:Panel>
</center>
</div>
function whdg_CellValueChanging(sender, eventArgs) {
if (eventArgs._cell._column._key == "code") {
var msgErreur = valideCode(eventArgs._newValue);
if (msgErreur != "") {
afficheMsgBox(msgErreur);
eventArgs.set_cancel(true);
;
} else {
//Formate sur 3 decimales
eventArgs._newValue = zeroPadding(eventArgs._newValue, 3);
}
function zeroPadding(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
//Retour: message d'erreur si la valeur entree par l'usager est invalide OU si retourne rien, c'est que la valeur entree par l'usager est valide
function valideCode(texteUsager) {
var typeErreur = 0; //0: pas d'erreur, 1: pas un nombre valide entre le min et le max sur 3 digit
var msgErreur = "";
// texteUsager est Null si l'usager entre juste le symbole "-" ou n'entre rien
if (texteUsager == null) {
typeErreur = 1;
if (isNaN(texteUsager)) {
if ((texteUsager < 0) ||
(texteUsager >= 900)) {
var strTexteUsager = "".concat(texteUsager);
if (strTexteUsager.length != 3) {
if (typeErreur == 1) {
msgErreur = "Veuillez entrer un nombre positif entre 000 et 899 inclusivement composé de trois chiffres uniquement.";
return msgErreur;
function afficheMsgBox(msgErreur) {
document.getElementById("MainContent_pnlBoxMessage").style.display = "block"; // Show pnl
document.getElementById("MainContent_lblboxMessage").innerHTML = msgErreur;
'*****SERVER SIDE*****
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Expires = -1 ' Empeche le browser de mettre la page en cache...
sessionInfo() 'Retrieve session info
If (Not IsPostBack) Then
'Supprime la variable session lors d'entre dans la page
If Session("DataSESWBS") IsNot Nothing Then
Session.Remove("DataSESWBS")
End If
bindGrid()
End Sub
Function fillData(enId As Integer) As DataSet
Dim strSQL As String = ""
Dim ParentCols(1) As DataColumn
Dim ChildCols(1) As DataColumn
objDS = Nothing
objDS = New Data.DataSet
Dim vWhereCP As String = ""
'*******************************************************
'NIVEAU 1
strSQL = "SELECT "
strSQL += "id, "
strSQL += "code, " ' "RIGHT( '000' + CAST(code AS VARCHAR(3)) , 3) AS code, "
strSQL += "nom_FR AS nom "
strSQL += "FROM "
strSQL += "SES.WBS_Niv1 "
strSQL += "WHERE "
strSQL += "ref_en_id=" & enId.ToString & " AND DeletedDate IS NULL "
strSQL += "ORDER BY code,nom_FR"
c_clsSQL_SES.Fill_RO(strSQL, objDS, "WBS_Niv1")
Dim pkeyNiv1(0) As DataColumn
pkeyNiv1(0) = objDS.Tables("WBS_Niv1").Columns("id")
objDS.Tables("WBS_Niv1").PrimaryKey = pkeyNiv1
'NIVEAU 2
strSQL += "N2.id, "
strSQL += "N2.ref_WBS_Niv1_ID, "
strSQL += "N2.code, " ' "RIGHT( '000' + CAST(N2.code AS VARCHAR(3)) , 3) AS code, "
strSQL += "N2.nom_FR AS nom "
strSQL += "SES.WBS_Niv2 AS N2 "
strSQL += "INNER JOIN SES.WBS_Niv1 AS N1 ON N1.id=N2.ref_WBS_Niv1_ID "
strSQL += "N1.ref_en_id=" & enId.ToString & " AND N2.DeletedDate IS NULL "
strSQL += "ORDER BY N2.code,N2.nom_FR"
c_clsSQL_SES.Fill_RO(strSQL, objDS, "WBS_Niv2")
Dim pkeyNiv2(0) As DataColumn
pkeyNiv2(0) = objDS.Tables("WBS_Niv2").Columns("id")
objDS.Tables("WBS_Niv2").PrimaryKey = pkeyNiv2
objDS.Relations.Add("WBS_Niv1_WBS_Niv2", objDS.Tables("WBS_Niv1").Columns("id"), objDS.Tables("WBS_Niv2").Columns("ref_WBS_Niv1_ID"))
Session("DataSESWBS") = objDS
If objDS.Tables("WBS_Niv1").Rows.Count <> 0 Then
btnImporterSES.Enabled = False
Return objDS
End Function
Sub bindGrid()
whdg.DataSource = TryCast(Session("DataSESWBS"), DataSet)
Else
whdg.DataSource = Nothing
whdg.GridView.ClearDataSource()
whdg.DataSource = fillData(CInt(lblSESID.Value))
whdg.DataBind()
SetHoursInGrid()
Sub SetHoursInGrid()
'Retenir si chaque SDP de chaque niveau a des heures assignées ou pas
If whdg.GridView.Rows.Count > 0 Then
Dim parentID As Integer
For i As Integer = 0 To (whdg.GridView.Rows.Count - 1)
parentID = CInt(whdg.GridView.Rows(i).Items.FindItemByKey("id").Text)
whdg.GridView.Rows(i).Items.FindItemByKey("HasHours").Text = CStr(SES.sqlWBS.isWbsWithHour(CInt(lblSESID.Value), parentID, 0))
If whdg.GridView.Rows(i).RowIslands(0).Rows.Count > 0 Then
Dim childID As Integer
For j As Integer = 0 To (whdg.GridView.Rows(i).RowIslands(0).Rows.Count - 1)
childID = CInt(whdg.GridView.Rows(i).RowIslands(0).Rows(j).Items.FindItemByKey("id").Text)
whdg.GridView.Rows(i).RowIslands(0).Rows(j).Items.FindItemByKey("HasHours").Text = CStr(SES.sqlWBS.isWbsWithHour(CInt(lblSESID.Value), parentID, childID))
Next
I forgot to mention in my reply that it only doesnt work for AddedRows where the 089 is added as 89 in the database. When i enter 087 in a row that already exists in the database, 087 is saved in the database.
Ok the reply i'm talking about doesnt appear. I said in it that i solved most of my problems by adding the following function, so there is only one problem yet, which i talk about in my other reply.
function whdg_RowAdding(sender, eventArgs) {
//Si le code est vide, on n'ajoute pas la ligne (le code serait 0)
if (eventArgs._cellValues[1].Text == "") {
I also added properties DataFormatString="{0:000}" DataType="System.String" to the 2 BoundDataField whose Key is "code". Doesn't change anything.
I found my last solution by formatting the value for my SQL clause and also when i place the value my dataset shown in my grid, using:
Dim objCmd As SqlClient.SqlCommand = Nothing
`...
objCmd.Parameters.AddWithValue("@code", codeValue.ToString("000"))
Case closed!!! :-D
NOOOOOO! Talked too fast. Now I cant save the new values. Lets forget my last solution and help me to find a new one.
Ok I use a string variable instead of a Decimal variable to memorize the formatted value of the RowAdded. My error. I'm currently solving another bug by myself, so everything is fine for me.
Hello,
I am glad you have managed to resolve your issue.
Let me just note that DataFormatString affects only the way you display the value, but it does not change the value itself. Value of 2 will display as 02 in a cell with DataFormatString = "{0:00} and will show as 003 in a cell with DataFormatString = "{0:000}.
Please let me know if you have any further questions.