Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How can i control the datagridview data in 2 feild not s
Message
De
11/11/2011 08:38:31
 
 
À
11/11/2011 05:18:53
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows XP
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01528543
Message ID:
01528650
Vues:
35
>the database already exits reocrd HKD USD 7.8 in database and datagirdview
>
>The user entry HKD to field 1 after entry USD to field 2 in datagirdview then will trigger the Handles dg.CellValidating. pop error message
>or
>user entry USD to field 2 after entry HKD to field 1 , then will trigger the Handles dg.CellValidating,pop error message
>
>
>
>Because USD convert to HKD is 7.8 already exits.
>So user can't again entry HKD USD

Something like this in cell validation should work:
DataGridView dg = sender as DataGridView;
            string newValue = e.FormattedValue.ToString();

            if (e.ColumnIndex != 1 && e.ColumnIndex != 0) return;

            if (newValue != string.Empty)
            {
                 int otherCell = e.ColumnIndex==0 ? 1 : 0;
                string sameRowValue =  dg.Rows[e.RowIndex].Cells[otherCell].FormattedValue.ToString();

                if (newValue == sameRowValue)
                {
                    MessageBox.Show("Duplicate Currencies");
                    e.Cancel = true;
                    return;
                }

                if (sameRowValue != string.Empty)
                {
                    string checkA = newValue + sameRowValue;
                    string checkB = sameRowValue + newValue;

                    var v = from DataGridViewRow x in dg.Rows
                            where x.Index != e.RowIndex
                            select new {
                                    CheckA = x.Cells[0].FormattedValue.ToString() + x.Cells[1].FormattedValue.ToString() 
                                 };

                    var matches = (from xx in v where xx.CheckA == checkA || xx.CheckA == checkB select xx).FirstOrDefault();

                    if (matches != null)
                    {
                        MessageBox.Show("Row already present");
                        e.Cancel = true;
                    }
                }
            }
Assuming that you know the two columns holding the currency codes (0 and 1 in the above)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform