DataGrid possesses a very important status in ASP.NET programming, and DataGrid is almost indispensable for high-volume data in browsers. The commonly used method of setting the DataGrid property is implemented at the reception by the ASP statement, which is obviously not suitable for dynamic loading of DataGrid, and the function of the front desk is very important through the background C # code. The following is to illustrate the setting method.
Like other ways of other dynamically loaded server controls, you must render DataGrid on the client browser, you must have a PANEL container control to load it, the method is as follows: Panel panel1 = new panel ();
If necessary, you can also set other properties of the Panel. See the appendix for details.
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 a DataGrid control: DataGrid myDataGrid = new dataGrid (); if the data of each column is not automatic load Instead, it is time-integrated, then you need to add the following code: mydatagrid.autogeneratecolumns = false; set the title, binding byte with the following statement, and join the MyDataGrid control: BoundColumn S1 = new boundcolumn () S1.DataField = "EMPLOYEEID"; S1.Headertext = "Employee ID"; MyDataGrid.columns.Add (S1); MyDataGrid location is set by code: myDataGrid.style ["position"] = "absolute"; myDataGrid. Style ["Left"] = "0px"; MyDataGrid.style ["TOP"] = "0px"; where the LEFT and TOP attributes are relative to the relative position of the above PANEL1. Since DataGrid is very rich, its property settings are very complicated, including background color and font size such fine links can be set according to the user's definition, and the detailed property setting code is shown in Appendix. It is worth noting that cellpadding is used to get the amount of space between the contents of the cell and the cells of the cell; cellspacing is used to obtain the amount of space between the cells; SelectEMStyle is used to obtain the current selected item style properties ItemStyle is used to get the style properties of the items in the MyDataGrid control; Headerstyle is used to get the style properties of the title section in the MyDataGrid control; the style property ensures that the Chinese of the Header title section is displayed in one line. Add a MyDataGrid control to Panel1 by following the following code: Panel1.controls.add (MyDataGrid); Finally, you need to connect the relevant database and load the data in the database to the data source of MyDataGrid, see Appendix. At this time, the server control DataGrid is completed all tasks added in the background.
Appendix: Source code for dynamically loaded 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 = "Last name"; 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 (); s5.datafield = "Birthdate"; s5.Headertext = "birthday"; mydatagrid.columns.add (S5); BoundColumn s6 = new boundcolumn (); s6.datafield = "address; s6.headertext =" address "; myDataGrid.columns. Add (S6); MyDataGrid.style ["position"] = "absolute"; mydatagrid.style ["left"] = "0px"; mydatagrid.style ["TOP"] = "0px";
myDataGrid.BorderColor = Color.FromName ( "# DEBA84"); myDataGrid.BackColor = Color.FromName ( "# DEBA84"); myDataGrid.Style [ "BorderStyle"] = "None"; myDataGrid.Attributes.Add ( "BorderStyle" , "None"); myDataGrid.CellPadding = 3; myDataGrid.CellSpacing = 2; myDataGrid.Attributes.Add ( "BorderWidth", "1px"); myDataGrid.PageSize = 2; myDataGrid.AutoGenerateColumns = true; myDataGrid.SelectedItemStyle.Font .Bold = true; myDataGrid.SelectedItemStyle.ForeColor = Color.White; myDataGrid.SelectedItemStyle.BackColor = Color.FromName ( "# 738A9C"); myDataGrid.ItemStyle.ForeColor = Color.FromName ( "# 8C4510"); myDataGrid.ItemStyle .BackColor = Color.FromName ( "# FFF7E7"); myDataGrid.ItemStyle.HorizontalAlign = HorizontalAlign.Center; myDataGrid.ItemStyle.Wrap = false; myDataGrid.HeaderStyle.Font.Bold = true; myDataGrid.HeaderStyle.ForeColor = Color.White ; myDataGrid.HeaderStyle.BackColor = Color.FromName ( "# A55129"); myDataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; myDataGrid.HeaderStyle.Wrap = false; myDataGrid.AutoGenerateColumns = false; myDataGrid.HorizontalAlign = HorizontalAlign.Center; myDataGrid. PagerStyle.HorizontalAlign = HorizontalAlign.Center; myDataGrid.PagerStyle.Wrap = false; myDataGrid.Attributes.Add ( "style", "word-break: keep-all; word-wrap: normal"); Panel1.Controls.Add (myDataGrid );