Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
scrolling the datagridview in c#(windows app)automatically,can i do this???????plz if any idea
Posted
Updated 3-Dec-11 7:36am
v3
Comments
thatraja 2-Dec-11 22:41pm    
Need more details, show the code
Himu from Orissa 2-Dec-11 23:54pm    
i m populating a grid from database using datasource pproperty of grid,the grid is having 300 rows, i want that the grid should scroll automatically from top to bottom and after that it should go to up again...how i can do it??????
Himu from Orissa 3-Dec-11 13:00pm    
Here is my code......and it selects 300 records
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from vacseat order by ndate";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Himu from Orissa 3-Dec-11 13:01pm    
conn is connection object......i.e
sqlconnection conn=new sqlconnection();

Take a look at this StackOverflow Q/A How do I programmatically scroll a winforms control?[^]
 
Share this answer
 
Comments
BillWoodruff 3-Dec-11 19:15pm    
+5 an excellent referral !
Wonde Tadesse 3-Dec-11 19:17pm    
Thanks
Himu from Orissa 4-Dec-11 9:59am    
thanx it helped me
Wonde Tadesse 4-Dec-11 13:00pm    
You 're welcome
1. Add a Timer to your Windows Form project. Set its 'Interval property to the value in milliseconds you want.

a. create an integer variable, and set its value to the index of the row you want initially displayed at the top of the DataGridView.
int firstRowDisplayed = 200;
b. create an integer variable whose value is total number of rows -1.
int lastRowIndex = dataGridView1.Rows.Count -1;
2. use the integer FirstDisplayedScrollingRowIndex property (has get, and set) of the DataGridView to make sure the initial row you want displayed is visible.
dataGridView1.FirstDisplayedScrollingRowIndex  = firstRowDisplayed;
3. trigger the Timer in whatever way you wish (by Button EventHandler, in a Form Load or Shown EventHandler, whatever).

4. in the Timer Code 'Tick EventHandler:

a. do the right thing to increment the firstRowDisplayed variable and select the topmost visible row in the DataGridView. Incrementing will essentially scroll the DataGridView "up."

5. So, how do I make it go up, until the last row is at the top, and then back down until the first row is at the top ?

a. left for you to figure out :)

Hint: have a boolean variable which you will use inside the 'Tick EventHandler to determine whether you increment or decrement 'firstRowDisplayed based on comparing if its current value is equal to the last row (in which case firstRowDisplayed == lastRowIndex), and you the want to start scrolling back to the top, or (if moving up) is equal to zero, in which case you to start scrolling back towards the bottom.

6. plan for how you wish to determine when the "animation" will stop, at which point you will disable the Timer.
 
Share this answer
 
v4
Comments
Wonde Tadesse 3-Dec-11 19:18pm    
5+.Good explanation.
Himu from Orissa 4-Dec-11 9:59am    
its what i was looking for
Have you tried adding a panel, then having its size fixed, then making it scrollable?? then placing your datagridview inside it?? Try this one. I believe this will solve your problem.

Regards,
Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900