Results 1 to 7 of 7

Thread: Limiting the DataGridView Rows!!!

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Limiting the DataGridView Rows!!!

    I had a DataGridView in which i want to fetch the data from the access database,so i did this code:
    Code:
     Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Etech.mdb"
    
            Dim myConnection As OleDbConnection = New OleDbConnection
    
            myConnection.ConnectionString = connString
    
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select LAcNo,IssuerName,Creditor,Amount,ROI,EMIAmount,BankAcNo,CreditCardNo,Notes from MLoan", myConnection)
    
            Dim ds As DataSet = New DataSet
    
            DataGridView1.Columns(0).DataPropertyName = "LAcNo"
    
            DataGridView1.Columns(1).DataPropertyName = "IssuerName"
    
            DataGridView1.Columns(2).DataPropertyName = "Creditor"
    
            DataGridView1.Columns(3).DataPropertyName = "Amount"
    
            DataGridView1.Columns(4).DataPropertyName = "ROI"
    
            DataGridView1.Columns(5).DataPropertyName = "EMIAmount"
    
            DataGridView1.Columns(6).DataPropertyName = "BankAcNo"
    
            DataGridView1.Columns(7).DataPropertyName = "CreditCardNo"
    
            DataGridView1.Columns(8).DataPropertyName = "Notes"
    
            da.Fill(ds, "MLoan")
    
            Dim dt As New DataTable
    
            da.Fill(dt)
            
            DataGridView1.DataSource = dt
    Now after fetching the data from the database to the DataGridView,i want to remove those columns of the DataGridView where the columns does not contain any value;
    i.e,the columns with no contents......
    Initially my design form looks like this:
    Attachment 73025
    After the load button click it looks like this:
    Attachment 73026
    In the above snapshot,after fetching the data from the access database to the DataGridView,i want to remove the PplicyHolderName,PremiumAmount and a few other columns since these are the columns which do not contain any data....
    How to do this?
    Thank you
    Last edited by gautamshaw; Feb 21st, 2010 at 01:21 PM.

  2. #2
    Member
    Join Date
    Sep 2009
    Posts
    32

    Re: Limiting the DataGridView Rows!!!

    Hi,

    Did you try:
    Code:
    DataGridView1.Columns(8).Visible=False

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Limiting the DataGridView Rows!!!

    Would it not be better to just exclude those columns from the query in the first place, or do you need them for other purposes?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Limiting the DataGridView Rows!!!

    ya jmc,i cant exclude those columns in the first place since i need them for some other purpose..
    i think this code:
    Code:
    DataGridView1.Columns(8).Visible=False
    is not bad.......
    I thought a lots of this and that except the easiest way to do the needful.......

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Limiting the DataGridView Rows!!!

    Let's think about this. What would be the better option:

    1. Create all the grid columns and then hide some of them, thereby creating a whole bunch of objects that will never be used.

    2. Create only the columns that are required in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Limiting the DataGridView Rows!!!

    ya jmc....you are right
    the 2nd one is a better option

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Limiting the DataGridView Rows!!!

    You would set the AutoGenerateColumns property of the grid to False, which can only be done in code and obviously must be done before the DataSource is set, which is when the columns would be automatically generated. You would then add only the columns you need at design time. You set the DataPropertyName of each grid column so it knows which table column to bind to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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