Server-side dynamically load the DataGrid control and set its properties

Source: Internet
Author: User
Tags add header relative
Datagrid|datagrid Control | dynamic | server | Load |datagrid|datagrid Control | server | load The DataGrid occupies a very important place in asp.net programming, and the DataGrid is almost indispensable for the presentation of large amounts of data in browsers. Commonly used to set the DataGrid property is implemented in the foreground by the ASP statement, so obviously not suitable for the DataGrid dynamic loading, through the background C # code to realize the function of the foreground is very important, the following is an example to illustrate its setting method.
As with other methods of dynamically loading server controls, to render the DataGrid on the client browser, you must have a Panel container control to load it, which is generated as follows: Panel panel1=new panel ();
If necessary, you can also set other properties of the panel, as detailed in the appendix.
If the size of the panel does not change with the size of the DataGrid, you need to add the following code:
panel1.style["OVERFLOW"]= "Auto";
Create DataGrid control: DataGrid mydatagrid=new DataGrid ();
If the data for each column is not automatically loaded, but is manually bound, you need to add the following code:
Mydatagrid.autogeneratecolumns=false;
Use the following statement to set the title of each column, the bytes bound, and add to the Mydatagrid control:
BoundColumn s1=new BoundColumn ();
S1. Datafield= "EmployeeID";
S1. headertext= "Employee ID";
MYDATAGRID.COLUMNS.ADD (S1);
The location of the Mydatagrid is set by the following code:
mydatagrid.style["Position"]= "absolute";
Mydatagrid.style["left"]= "0px";
mydatagrid.style["Top"]= "0px";
Where the left and top properties are relative positions relative to the Panel1 above.
Because the DataGrid performance is very rich, so its property settings are very complex, including background color and font size, such as small links can be set according to the user's definition, the detailed property settings code see Appendix. Notably, cellpadding is used to get or set the amount of space between the contents of the cell and the border of the cell; cellspacing is used to get or set the amount of space between cells; SelectedItemStyle to get the style properties of the currently selected item ItemStyle is used to get the style properties of the items in the Mydatagrid control, HeaderStyle to get the style properties of the caption section in the Mydatagrid control, and the style property to ensure that the header header portion of Chinese is displayed in one line.
Add the Mydatagrid control to the PANEL1 with the following code:
PANEL1.CONTROLS.ADD (Mydatagrid);
Finally, you need to connect the database and load the data into the Mydatagrid data source, as detailed in the appendix. This completes all the tasks that the server control DataGrid adds in the background.
Appendix: Dynamically loading the source code for the DataGrid control:
panel1.style["Position"]= "absolute";
panel1.style["Top"]= "30px";
Panel1.style["left"]= "100px";
panel1.style["Width"]= "500px";
panel1.style["Height"]= "300px";

panel1.style["OVERFLOW"]= "Auto";
DataGrid mydatagrid=new DataGrid ();
BoundColumn s1=new BoundColumn ();
S1. Datafield= "EmployeeID";
S1. headertext= "Employee ID";
MYDATAGRID.COLUMNS.ADD (S1);
BoundColumn s2=new BoundColumn ();
S2. Datafield= "LastName";
S2. headertext= "Surname";
MYDATAGRID.COLUMNS.ADD (S2);
BoundColumn s3=new BoundColumn ();
S3. Datafield= "FirstName";
S3. headertext= "Name";
MYDATAGRID.COLUMNS.ADD (S3);
BoundColumn s4=new BoundColumn ();
S4. Datafield= "Title";
S4. headertext= "title";
MYDATAGRID.COLUMNS.ADD (S4);
BoundColumn s5=new BoundColumn ();
&



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.