CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    How to display large number of rows in a DataGridView?

    Hi,

    I have a stored procedure which gives around 8000 rows.
    While datagridview displays smaller number of rows from another stored procedure, it times out to display the results for this one!!!

    Any help is greatly appreciated..
    Yes, Possible!!

  2. #2
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: How to display large number of rows in a DataGridView?

    I don't have many specifics as I've yet to delve into this a lot yet, but research the VirtualMode property on the DataGridView and see how you can use it. VirtualMode only renders the rows the user can view at the time. When the user scrolls it will retrieve the new rows as they scroll instead of overloading the control in the beginning.

    I keep meaning to look this up and implement it on some of my controls in my app but haven't yet had the time.

  3. #3
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: How to display large number of rows in a DataGridView?

    DataGridView for sure can show 8000 rows of data for quite a while. But what you should think about is users, how do they get what they want after long time scrolling up and down. It's too hard for them, not practical way.

    I think you could put in more conditions to limit result data, or page the result data.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  4. #4
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: How to display large number of rows in a DataGridView?

    Page the result data ?

    Can you give an example?

    Thanks.
    Yes, Possible!!

  5. #5
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: How to display large number of rows in a DataGridView?

    Hi,

    Just figured out that - OdbcCommand property CommandTimeOut needs to be set up. By default it is 30 sec. So if the stored procedure takes more than that, you need to set it up.
    Example:

    Code:
     OdbcConnection thisConnection = getConnection();
     OdbcCommand nonqueryCommand =   thisConnection.CreateCommand();
     nonqueryCommand.CommandTimeout = 300;
    Hope it will be useful..
    Last edited by dasm; May 2nd, 2008 at 02:07 PM.
    Yes, Possible!!

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to display large number of rows in a DataGridView?

    Read the values into an array, or list, either at start up, or as needed. Then, wait for the user to press the first letter of the name he wants, and then show them.

    As he types another letter, show less and less records that match the result.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to display large number of rows in a DataGridView?

    What is the use of showing 8000 rows at a time. That would not make any sense unless it is a report. You should look at Pagination.

  8. #8
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: How to display large number of rows in a DataGridView?

    Hi ,

    Actually , the problem was to fetch the result itself before even displaying it!!. The stored proc takes more time than 30 sec..
    Now that it is given required time to fetch the result, i can see that the datagridview displays all the 8000 rows..
    Yes, now i have to think of displaying it in pages.. how to do pagination, by the way.. !
    dglienna' idea of displaying based on the user selected letter is also a nice one.. but if there is standard property or method for datagridview to page, i do not know .. can someone help??
    Yes, Possible!!

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to display large number of rows in a DataGridView?

    I would build this logic into stored procedure itself. The Stored procedure used to retrieve data would then give me results in pages. This is better and more efficient way of doing this. All you would have to do is provide couple of buttons for Previous and Next.

    The stored procedure would take two extra parameters, one for the number of records per page and second that will tell the Stored Procedure which page to return. I don't have enough time on hand to write code for you, but then if you check the database that you are using there might be several examples of Pagination present on the internet. Just google them.

    Now in case you don't want to tweak your stored procedure, just look at the overload of the Fill() method of the dataadapater. You will see an overload that lets you select the number of records for each fill. Take a look http://msdn.microsoft.com/en-us/libr...pter.fill.aspx

  10. #10
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: How to display large number of rows in a DataGridView?

    Thanks for the reply - but i wanted fastest way.. just if someone has the code ready..
    Its just a dumb idea to know about search engine for different options...
    I will post the code here once i am done with it.
    Last edited by dasm; May 4th, 2008 at 01:19 PM.
    Yes, Possible!!

  11. #11
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: How to display large number of rows in a DataGridView?

    Hi,
    Here is one way to paginate large number of rows in datagridview. Create two text boxes and pass the starting position and ending position. The OdbcAdapter- fill method takes parameter for starting and ending row position.
    Here is the code : (someone may find it useful !!)



    Code:
    public void execute(DataGridView d, ComboBox c1,ComboBox c2,ComboBox c3)
            {
                string dbname = c2.SelectedItem.ToString(); //pass database name from the list of databses you are working on
                OdbcConnection thisConnection = getConnection(dbname);
                OdbcCommand nonqueryCommand = thisConnection.CreateCommand();
                nonqueryCommand.CommandTimeout = 300;
    
    /* initialize variables: startPos and endPos from the textboxes' Text in the event of submit button. Get the values of them from get method */
    
                int startPos = getStartPos(); 
                int endPos = getEndPos();
    
                try
                {
                    thisConnection.Open();
                    
                    nonqueryCommand.CommandText = "your stored procedure";
                    nonqueryCommand.CommandType = CommandType.StoredProcedure;
                    
                    OdbcDataAdapter da = new OdbcDataAdapter(nonqueryCommand);
                    DataSet ds = new DataSet();
    
                      if (startPos != 0 && endPos != 0)
                    {
                        MessageBox.Show("Display Record from " + startPos.ToString() + " to " + endPos.ToString());
    
                        da.Fill(ds, startPos, endPos, "TableA");   //It happens here
    
                    } 
                   else 
                    {
                        da.Fill(ds, "TableA");    //display all if startPos and endPos not set.
                    }
    
                    ds.Tables["TableA"].DefaultView.AllowNew = false; 
                    
    
                    d.AutoGenerateColumns = true;
                    d.DataSource = ds.Tables[0];
                    thisConnection.Close();
                }
                catch (OdbcException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    
            }
    Last edited by dasm; May 6th, 2008 at 09:32 AM.
    Yes, Possible!!

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

Featured