Monday, August 16, 2010

Datagrid in VB.Net?

I have a datagrid that retrieves information from the database and below the datagrid there are several other controls. When the datagrid is displayed, sometimes it will be too long that it overlapped the controls below the datagrid, how can I make the controls below the datagrid flexible so when the datagrid is displayed the controls below the datagrid should move below according to the length of the datagrid? I will not know how long the datagrid will be because it is retrieved from the database and sometimes it might be short and sometimes it might be long, so i can't set the controls to a specific place. How can I solve this? I've tried putting the datagrid in a table row and adding the controls to the next row of the table but the datagrid will have error. Thanks, any help will be greatly appreciated!Datagrid in VB.Net?
In Visual Basic .Net the DataGrid control has events called DataGrid1_Resize() and Location_Changed().





Place code in the DataGrid's Resize() event to cause other controls beneath it to shift their positon anytime the DataGrid's dimensions change.





Hint: You might consider placing the controls that are located beneath the DataGrid into some sort of container, i.e., Panel. In that way you just have to reset the Top property of just one control, versus several.





For Example:





Private Sub Resize()


Dim intTop as Integer


Dim intHeight as Integer





intTop = DataGrid1.Top


intHeight = DataGrid1.Height





'Reset the Top properties of the controls beneath DataGrid1


thisControl1.Top = intTop + intHeight + 25


End SubDatagrid in VB.Net?
If you put the DataGrid in side of a table cell then everything should automatically happen for you.





%26lt;table border=';0';%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;


%26lt;asp:DataGrid ID=';DataGrid1'; runat=';server'; ...%26gt;


...


%26lt;/asp:DataGrid%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;


%26lt;asp:Button ID=';Button1'; runat=';server'; Text=';Submit'; /%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;/table%26gt;





This should place the button directly below the datagrid regardless of size.





If you want the buttons to be the same place on the screen regardless then you should wrap a div tag around your data grid and give it a specific height attibute in th style attribute. The you can turn overflow to auto on and your table will scroll within the page that is displayed.
Please use a panel control and place the grid on it. Like keep the other control on another panel below the grid
I have used the datagrid many times and I truly never like it. I use the MSFlexGrid and it seems to respond much better. Plus it has scroll bars for going to the right for extended data. You can also set check boxes, pull downs, etc on the MSFlexgrid to allow for selections and then you can write code to update the rows and fields that have been changed.





Sounds like though for this if your stuck using the datagrid would be to put your controls before the dataset and then see if they stay. Not sure how it looks to really describe some options for you.
I would use the datagridview control rather then the datagrid control. But... if you still want to use the datagrid control, I am pretty sure you can fix the size of it. If not, then another option would be to get the size and coordinates of the datagrid control once rendered and re-locate the other controls based on the location of the datagrid. To re-locate the other controls just take the coordinates of the specific part of the datagrid and add that to the location of the control to be moved. I can't remember the specific properties that you use to do this, but I have done this before with other controls.

No comments:

Post a Comment