Results 1 to 3 of 3

Thread: Datagridview validation

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Datagridview validation

    What is the best way to validate whether or not information exists in a datagrid. Let me give a situation.

    What I'm trying to do here is allow the user to search and sort the dgv based off of the character or string that is he/she inputs into a textbox. The things I want to validate are:

    The textbox is not blank, and if not blank has valid information. (easy enough)

    The dgv has actually been populated from the dataset!!!!!

    The second I can't seem to nail down, regardless of what I use. I have tried using rowcollections, columns.count(), cell, item, and a few other properties all of which either get skipped over by my if statements or throw an index is out of range error. Can anyone give me a hand with this?

    Thank you.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Datagridview validation

    1) Checking if the TextBox is not blank is easy, just check if the TextBox.TextLength is not 0. Valid information depends on what you define as valid information. Could it only contain certain letters, or maybe numbers only? Only lowercase and so on...

    2) There are a few ways of checking if the DataGridView contains information. Firstly, I guess your DGV is bound to a DataTable. First thing to check could be the DataSet.Rows.Count-propery, or the DataGridView.Rows.Count-property to see if it contains any rows at all. If the count is zero, there is no data there.

    But, what I would do is this... Since you are going to sort the DGV, you will sort it by a column, right? For that reason, it would be more clever to check if the column actually exists before sorting the DGV. If it doesn't exists, it could be that your DataTable hasn't been populated properly. To check if it exists, you can do:
    Code:
    If Not DataGridView1.Columns.Contains("ColumnName") Then
    MessageBox.Show("Column doesn't exists")
    Else
    ' Do your thing
    End If
    Does this help you?

  3. #3
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Datagridview validation

    1. To check if there is nothing in the textbox, use:

    Code:
    Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) handles TextBox.Validated
    
    if TextBox.Text = "" then
      TextBox.Focus()
    end if
    
    End Sub
    2. If you have bound the dataset to the datagridview, then:

    Code:
    tableadapter.Fill(recordset.dataset)
    So if there is anything in the dataset, then the Datagridview will be populated. To sort the Datagridview, you could either do that programmatically or just get the user to click on the Column header.

    To do it programmatically :

    Code:
    datagridview.Sort(columnname, System.ComponentModel.ListSortDirection.Ascending)
    
    or
    
    datagridview.Sort(columnname, System.ComponentModel.ListSortDirection.Descending)
    I hope this is of use.

    Computerman
    It was much easier in VB6, but I am now liking Vb.Net alot more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width