Aspxgridview addition, deletion, modification, query, data binding, appearance display, function settings, pagination
I. Aspxgridview display
Attribute:
Caption ---- Column Title (
Keyfieldname ---- database Field
Does seofriendly enable search engine optimization?
The format of the page summary information specified by summary.
Setting node showfilterrow = true to set the quick search function
Settingsbehavior. allowfocusedrow = true highlight the selected row, that is, the color of the selected row
Settingsbehavior. alldragdrop = false do not drag the title column header
Settingsbehavior. allowsort: whether the column header can be sorted after being clicked
Settingspager. showemptydatarows = true; when data behavior is empty, empty rows are displayed.
Settingspager. pagesize total number of records displayed per page
The text of the allbutton. Text "show all data" button
Allbutton. Visible: whether to display the "show all data" button
Firstpagebuotton/lastpagebutton/nextpagebutton/prevpagebutton/Corresponds to the home page, last page, next page, and previous page. The settings are the same as above.
The minimum value of numericbuttoncount is 1, which controls the number of displayed pages.
Protected void aspxgridview1_pageindexchanged (Object sender, eventargs E)
{
Databind (); // you only need to re-bind the data to turn pages up and down
}
The new column is of the gridviewdatatextcolumn type by default. Select change to in the toolbar to change the column type. You can change the editing method when adding or modifying a column.
Set the date type display format. In "behavior" propertiesdateedit -- displayformatstring -- Example: {0: yyyy Year mm Month}
When show Group panel is selected, the focusedrowchanged event is used to re-bind the data. Select the row before viewing the data.
Protected void aspxgridview1_focusedrowchanged (Object sender, eventargs E)
{
Getdata ();
} Disable editing of a column. The behavior of this column is editformsetask-visible = false. Hide the add, delete, and update buttons of Edit columns in the code.(Aspxgridview1.columns [edit column] As gridviewcommandcolumn). newbutton. Visible = true;(Aspxgridview1.columns [edit column] As gridviewcommandcolumn). deletebutton. Visible = true;(Aspxgridview1.columns [8] As gridviewcommandcolumn). updatebutton. Visible = true; Each row has a checkbox, which can be dynamically selected.<Columns>
<DX: gridviewcommandcolumnShowselectcheckbox = "true"Visibleindex = "8"> </dx: gridviewcommandcolumn>... other columns</Columns>
II. Aspxgridview Binding data
Aspxgridview1.keyfieldname = "ID"; // specify the primary key. Directly update data and bind the sub-table
Aspxgridview1.datasource = DT. defaultview; // specify the data of the grid.
Aspxgridview1.databind (); // execute binding
Note: If the query result field has an alias, when editing this field, unboundtype should be set to object
3. Search for aspxgridviewFilter data and find data
Method 1: Expand the Filter list next to the column title to filter data (similar to the Excel Filter Method) grid. settings. showheaderfilterbutton = true; the Filter list lists all the data in the column. You can also customize the contents of the filter list, for usage see: http://demos.devexpress.com/ASPxGridViewDemos/Filtering/HeaderFilter.aspx
Method 2: display the field filter condition input box grid. settings. showfilterrow = true in the column header; display the condition judgment mode drop-down list grid. settings. showfilterrowmenu = true;
4. delete data
Protected void aspxgridview1_rowdeleting (Object sender, devexpress. Web. Data. aspxdatadeletingeventargs E)
{
E. Cancel = true; // otherwise, the deleted result can be viewed only when the page is refreshed.
Int id = convert. toint32 (E. Keys [0]); // obtain the ID
UPD. deldownfilelist (ID); // from {
Tagshow (event, '% Ca % fd % be % DD % BF % e2 ');
} "Href =" javascript:; "target =" _ Self ">DatabaseDelete record
Uploadfilelistbind (); // data table binding
}
The deletion prompt that comes with aspxgridview. set two attributes:
Settingsbehavior. ==> confirmdelete = true
Settingstext ==> confirmdelete = string to be prompted
5. Update
Use E. newvalues [Index]
Remember to update the data after aspxgridview1.canceledit (); // end the editing status
E. Cancel = true;
BIND ();
Example: // update
Protected void aspxgridview1_rowupdating (Object sender, devexpress. Web. Data. aspxdataupdatingeventargs E)
{
Bill. Message M = new bill. Message ();
// Use E. newvalues [Index] For the value
String id = convert. tostring (E. Keys [0]);
String event_date = E. newvalues [1]. tostring ();
String event_title = E. newvalues [2]. tostring ();
String event_description = E. newvalues [3]. tostring ();
String tag = E. newvalues [4]. tostring ();
M. message_update (ID, event_date, event_title, event_description, tag );
Aspxgridview1.canceledit (); // end the editing status.
E. Cancel = true;
BIND ();
}
Sort by six
Rebind data in the beforecolumnsortinggrouping event
7. Paging
Rebind data in the pageindexchanged event
1. Dynamic addition of non-data binding columns. Example: Dynamic addition of row number ColumnsIf (! Ispostback)
{// Dynamically add row numbers not bound to data
Gridviewdatatextcolumn DL = new gridviewdatatextcolumn ();
DL. Caption = "row number ";
DL. fieldname = "hh"; // HH field bound to this column
DL. unboundtype = devexpress. Data. unboundcolumntype. String; // The non-data binding type is bytes.
DL. propertiestextedit. displayformatstring = "C"; // display character format
DL. propertiestextedit. focusedstyle. forecolor = system. Drawing. color. Red;
DL. visibleindex = 0; // you can specify the index position of a row or column.
Aspxgridview1.columns. insert (0, DL); // insert row number column before 0
Getdata ();
Aspxgridview1.caption = "users whose IP address is" + getclientip () + "are viewing online banking terminal update content ";
} In the customunboundcolumndata event, protected void aspxgridview1_customunboundcolumndata (Object sender, aspxgridviewcolumndataeventargs E)
{
If (E. Column. fieldname = "hh" & E. isgetdata)
E. value = (E. listsourcerowindex + 1). tostring ();
} 2. A simple way to perform operations on the aspxcombobox column is: 1. filedname: Write the primary table to the table.
2. Find these attributes under propertiescombobox:
Then, bind the datasourceid in the column of the customer name to the objectdatasource of our word table.
Set the field name in textfield, for example: Name
Set the name in valuefield to be the primary key of the Word Table (that is, the foreign key of the Word Table referenced by the main table), for example: uid
In this way, you can easily bind the behavior-propertiescombobox-valuestype of the column to the system in aspx without writing code. string to avoid unselectable ComboBox using devexpress. web. aspxgridview;
Using devexpress. Web. aspxeditors; // assign a value to the combox column when loading the page. Here, workgroupid is the field bound to the combox column in aspxgridview.
(Aspxgridview1.columns ["workgroupid"] As gridviewdatacomboboxcolumn). propertiescombobox. valuetype = typeof (INT );
Fangqm. netbank. Core. groupinfo group = new fangqm. netbank. Core. groupinfo ();
Datatable dt = group. groupselectall (); // table
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Int id = convert. toint32 (Dt. Rows [I] [0]);
String v = DT. rows [I] [1]. tostring (); (aspxgridview1.columns ["workgroupid"] As gridviewdatacomboboxcolumn ). propertiescombobox. items. add (New listedititem (v, ID ));
} Update the table. E. newvalues [1]) to obtain the client value, for example, model. workgroupid = convert. toint32 (E. newvalues [1]); note that the combox column should be displayed first and then bound to data. The field binding is case sensitive and must be exactly the same as the field name of the SELECT statement. data Summary
Calculate the average or sum of all rows in aspxgridview and display them in the footer.
The footer is displayed only when the settings. showfooter attribute is set to true.
The summary entries of aspxgridview are placed in the totalsummary attribute. Set displayformat to {0] terminals in total,
Set fieldname to a non-bound field, and set summarytype To sum to calculate the sum
4. Hide the edit column. In the databound event, protected void aspxgridview1_databound (Object sender, eventargs E)
{
If (aspxgridview1.visiblerowcount> 0)
{
// Aspxgridview1.columns [command column Index]
(Aspxgridview1.columns [4] As gridviewcommandcolumn). newbutton. Visible = false;
}
}
Sat. Aspxgridview FAQs
A. Click the edit or new button and delete the update and cancel buttons. After the data is edited, click Update. An error occurs: "The specified method is not supported." solution:
1. Make sure that keyfieldname is set for aspxgridview
2. Make sure that the aspxgridview has defined the event onrowdeleting, onrowinserting, onrowupdating
3. The background code processes the onrowdeleting, onrowinserting, and onrowupdating events.
2. Bind a Master/Slave table (ilist)
The list element has the list attribute (category. Products) and needs to be displayed in the form of Grid Nesting.
1. Select the gridview (gird1), right-click the menu and choose "Edit template"-"detailrow". The detailed data page is displayed, and a new aspxgridview (grid2) is added to the detailrow to display detailed data, you can set columns attributes of grid2. Grid2.settingsdetail. isdetailgrid = true specify grid2 as the slave table data table.
2. added the grid2 databinding event.
Code
Protected void grid2_databinding (Object sender, eventargs E)
{
Devexpress. Web. aspxgridview. aspxgridview grid = sender as devexpress. Web. aspxgridview. aspxgridview;
If (grid! = NULL) & (dict! = NULL ))
{
Int I = (INT) grid. getmasterrowkeyvalue ();/* Get the key recorded in the master table. keyfieldname must be set for the master table grid */
If (I> = 0)
{
Grid. datasource = dict. Products; // data is located using the key, which indicates the child table data source.
}
}
}
3. Right-click detailrow and select "End template editing ". Modify grid1.settingsdetail attributes
Bool allowonlyonemasterrowexpanded defaults to false. whether to allow only one row of the primary table to expand. When the second line of detailed records is expanded after true, the last expanded detailed records are closed.
Whether the bool showdetailbutton displays the details button. True: displays a "+" line at the beginning.
Bool showdetailrow true displays detailed data
4. filter data
Method 1: Expand the Filter list next to the column title to filter data (similar to the Excel Filter Method) grid. settings. showheaderfilterbutton = true; the Filter list lists all the data in the column. You can also customize the contents of the filter list, for usage see: http://demos.devexpress.com/ASPxGridViewDemos/Filtering/HeaderFilter.aspx
Method 2: display the field filter condition input box grid. settings. showfilterrow = true in the column header; display the condition judgment mode drop-down list grid. settings. showfilterrowmenu = true;
5. Custom Column Display
Grid. settingcustomizationwindow
Enabled running custom Column Display
Popuphorizontalalign horizontal align
Popupverticalalign vertical align
Open the column editing box in JavaScript.
Code
<Script. type = "text/JavaScript">
<% -- Title of the change button -- %>
Function updatecustomizationwindowvalue (){
VaR element = Document. getelementbyid ("btncustwindow ");
If (element = NULL) return;
Element. value = (grid. iscustomizationwindowvisible ()? "Hide": "show") + "customization window ";
}
<% -- Display the Custom column editing window -- %>
Function showhidecustomizationwindow (){
If (grid. iscustomizationwindowvisible ())
Grid. hidecustomizationwindow ();
Else grid. showcustomizationwindow ();
Updatecustomizationwindowvalue ();
}
</SCRIPT>
<Input id = "btncustwindow" type = "button" value = "show customization window" nclick = "showhidecustomizationwindow ();"/>
<Dxwgv: aspxgridview id = "gird" ......>
................................
<Clientsideevents customizationwindowcloseup = "function (s, e) {updatecustomizationwindowvalue ();}"/>
</Dxwgv: aspxgridview>
TIPS:
When binding data to aspxgridview, if the data column type is date type, "gridviewdatacolumn" should be used instead of "gridviewdatatextcolumn". In this case, the data is displayed in the format of "" instead of "0:00:00 ".
The search control in aspxgridview cannot use findcontrol as in the gridview, but should use the findrowcelltemplatecontrol method.
Give up response. write () method to output characters, if the page calls response. the write method will cause the client sorting function of aspxgridview to lose the control. The specific manifestation is: When you click Sort, loading is displayed, and then no response is returned. It keeps loading and sorting is not completed.
When using aspxgridviewexporter to export data in aspxgridview, if there is a data column, although we can use
<Propertiesdateedit displayformatstring = "{0: yyyy-mm-dd}">
</Propertiesdateedit> to format the display style of the date column in The aspxgridviewexporter. However, we cannot control the style in the Excel file exported using aspxgridviewexporter. In this case, in the exported Excel file, the display format of the date column is the format of the date column in The aspxgridview data source in the database. To control the format of the exported Excel date column, you must start from the aspxgridview data source and format the date column in the data source to format the exported Excel file date column.
4. Export aspxgridview data
Add an aspxgridviewexporter control to the page and set the gridviewid as the aspxgridview of the data to be exported. Call the following method to export the data.
Aspxgridviewexporter1.writexlstoresponse ()
Aspxgridviewexporter1.writecvstoresponse ()
Aspxgridviewexporter1.writeappstoresponse ()
Aspxgridviewexporter1.writertftoresponse ()
For how to export the Excel date format, set the cells to the corresponding format.
1. traverse all data in the primary key column.
List <Object> keyValue = aspxgridview1.getselectedfieldvalues ("kid ");
String STR;
Foreach (Object key in keyValue)
{
STR = key. tostring ();
}
2. dynamically select a row.
// N indicates the number of rows to be selected
Aspxgridview1.selection. setselection (n, true );
3. obtain data from all selected rows. Check the data here.Http://space.itpub.net /? Uid-23109131-action-viewspace-itemid-676010
4. problem: when the aspxgridview cell contains a hyperlink button, the aspxgridview is sorted and the hyperlink is misplaced.
Solution: Set the enableviewstate attribute of the hyperlink button to false;
5. Based on the data of a row, the dynamic selection check box cannot be edited, and the row background color is dynamically set.
Protected void aspxgridview1_htmlrowprepared (Object sender, aspxgridviewtableroweventargs E)
{
// Determine whether the bomcode is empty.
If (E. getvalue ("bomcode"). tostring (). Trim () = "")
{
// The set and select check boxes cannot be edited.
E. Row. Cell [0]. Enabled = false;
// Set the background color to light gray
E. Row. backcolor = color. lightgray;
}
}
// There is a link address in the table to download the file from the link address and reference the namespace devexpress. Web. aspxgridview;
// If you do not want dynamic binding, you only need to set the fieldname of the column as the link address field, and textfield as the field that displays the Link name.
Protected void aspxgridview1_init (Object sender, eventargs E)
{
Gridviewdatahyperlinkcolumn collink = new gridviewdatahyperlinkcolumn (); // instantiate a hyperlink Column
Collink. Caption = "Download it"; // you can specify a column header.
Collink. propertieshyperlinkedit. Text = "this is a link"; // display the Link name.
Collink. propertieshyperlinkedit. textfield = "linkname"; // display the fields to be bound with the Link name
Collink. fieldname = "linkurl"; // fields bound to this column
Collink. propertieshyperlinkedit. navigateurlformatstring = "{0}"; // The link address is the field bound to this column.
Collink. Visible = true;
Collink. width = 200;
Aspxgridview1.columns. Add (collink); // Add this column to aspxgridview
}
Using system. Collections. Generic;
// Retrieve the set of current control values and directly find the control ID
List <Object> keyvalues = This. gridviewmethod. getselectedfieldvalues ("f_xxx"); // Control ID
Foreach (Object key in keyvalues) // looping
{
}
2. Get a row in aspgridview
List <Object> keyvalues = This. gridviewmethod. getcurrentpagerowvalues ("f_xxxxx"); // f_xxxxx is the primary key value.
Foreach (Object key in keyvalues) // cyclically traverses the data in each column of this row
{
}