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
10/11/2011 05:35:48
 
 
À
09/11/2011 23:54:16
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:
01528547
Vues:
25
>Hi all,
> I use vb.net and datagridview. I want to control 2 feild when user input the datagridview data is duplicate, then will pop message.
>For example, table A (a,b) . a b
> ----- -----
> 1 2
> 3 4
>
>When user entry 1 value in a field is ok , but entry 1 value in a field and 2 value in b field in same time ,then pop message.
>But i only know can check 1 field. Anyone know how can i do? Thank you.
>
>
>Private Sub dg_CellValidating(ByVal sender As Object, _
>            ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dg.CellValidating
>
>
>        Dim strErrMsg As String
>        For i = 0 To dg.Rows.Count - 1
>            dg.Rows(i).ErrorText = ""
>        Next
>
>        Try
>            With dg
>                If e.ColumnIndex = 0 Then
>                    If CStr(e.FormattedValue).Length = 0 Then
>                        strErrMsg = "a must entry!"
>                        .Rows(e.RowIndex).ErrorText = strErrMsg
>                        e.Cancel = True
>                        MsgBox("a must entry!", 48, "warning")
>                    Else
>                        Dim htTable As Hashtable = New Hashtable
>                        Dim iRow As Integer
>                        Dim sProductID As String
>                        Try
>                            .Rows(iRow).ErrorText = ""
>
>                            For iRow = 0 To .Rows.Count - 1               
>                                sProductID = _
>                                    Microsoft.VisualBasic.Left( _
>                                    CStr(.Rows(iRow).Cells(0). _
>                                    EditedFormattedValue) & Space(4), _
>                                    4)
>                                htTable.Add(sProductID, iRow)
>                            Next
>                        Catch ex As Exception
>                            strErrMsg = "a must not duplicate."
>                            .Rows(e.RowIndex).ErrorText = strErrMsg
>                            e.Cancel = True
>                            MsgBox("a must not duplicate!", 48, "warning")
>                        End Try
>                    End If
>                End If
>end sub
>
>
It looks as if you want to prevent duplicate values in any column. Something like this might be a starting point:
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridView dg = sender as DataGridView;
            string newValue = e.FormattedValue.ToString();

            if (newValue != string.Empty)
            {
                 var v = from DataGridViewCell x in dg.Rows[e.RowIndex].Cells
                        where
                            (x.ColumnIndex != e.ColumnIndex && x.Value != null
                             && x.FormattedValue.ToString()==newValue)
                        select x.DataGridView.Columns[x.ColumnIndex].HeaderText;

                foreach (string s in v)
                {
                    MessageBox.Show("Duplicate in " + s);
                    e.Cancel = true;
                }
            }
        }
C# I'm afraid but you can run it through a converter
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform