I have seen many identical or similar questions in the forum: How do I place check boxes, text boxes, etc. in every line of my DataGrid? How do I update their values? The answer is quite simple. In this article, I will show you how to complete it. We all know that DataGrid is a very powerful tool. According to my experience, in 90% of time, DataGrid is used to display data and may edit a line of data once. At some point, you may need to edit multiple lines, or even all data. A practical example is that in the application of online sales items, customers may change one or more items in their basket, click the check box to remove the goods they don't want. In this example, I wrote a simple WebForm to manage the list of contacts stored in XML. This requirement is very simple: with the ability to add new contacts, editing / deleting existing contacts. Users can modify or delete multiple contacts at a time, and I also allow users to sort the data grids according to their selected columns. My example is written in C #. If you prefer the VB versions of these code, there are two formats in the download file. Contacts.xml This example is very simple and intuitive. Because it is very simple, I have not created a plan.
myaddress@mycompany.com
John
DOE
YourAddress@yourcompany.com
Jane
DOE
ContactList.aspx Setting WebForm is very simple. I placed a new DataGrid into my form and set it to 4 columns, and the first column contains check boxes used to delete the contact. You will notice that the main job I do here is to create each column in the form of templatecolumn. This allows me to place text boxes and check box objects into the data template (ItemTemplate). This is a trick to display other things other than text in each line of the grid. In addition, you will notice that I use FooterTemplate to make new contacts simple and intuitive. I also contain a link button (LinkButton) to save the user's modification and delete operation. But it doesn't have to add new contacts. Add a new contact's operation by the Link button (LinkButton) in the footer template of the last column.
ContactList.cs When I choose to use the XML file to access the data, I decided to use DataSet to access it. This is a very reasonable choice because the DataSet object has ReadXML and WRITEXML methods. The first step is to read data in XML. As you can see from your code, I created a member to handle data sorting. private DataSet _dsContacts; private string _sSort;. private void Page_Load (object sender, System.EventArgs e) {// load an XML file _dsContacts = new DataSet (); _dsContacts.ReadXml (Server.MapPath ( "Contacts.xml")); Datacolumn [] dcpk = {_dscontacts.tables ["contact"]. Column ["email"]}; _dscontacts.tables ["contact"]. Primarykey = dcpk; if (! Page.ispostback) {// If it is the first During load, bind data. BindContacts (); _ssort = "firstname";} else {/ / otherwise, reading the sort status from the view state. _Ssort = (string) ViewState ["sort"];}} second step, I created a Binding the data to the grid, which contains the data sort logic and the method of reading data from the disk. Private void bindcontacts () {// Save Sort Status to View Status. ViewState ["Sort"] = _ssort; // Bind grid to the sorted data view. DataView DV = New DataView (_dscontacts.tables [" Contact "]); DV.Sort = _ssort; dgcontacts.datasource = DV; dgcontacts.database ();} private void savecontacts () {_dscontacts.writexml (Server.MAppath (" Contacts.xml "));} ItemMand Event To deal with add new contacts to the list. Note: I checked whether the commandName parameter is add. It is to process the return value of the link button (LinkButton) in the last column of the grid in the ASPX page.
Private Void DGContacts_ItemCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventAndargs e) {// Add new data to DataSet. Here I use an array to improve processing efficiency. if (e.commandname == "add") {string [] scontact = {"," ","}; scontact [0] = ((TextBox) E.Item.FindControl ("newemail")). Text; scontact [1] = ((TextBox) E.ITEM .FindControl ("newfirst"))). Text; scontact [2] = ((textbox) E.Item.FindControl ("newlast")). Text; _dscontacts.tables ["Contact"]. Rows.add (Scontact); SaveContacts ();} bindcontacts ();} I have skipped the SortCommand code, because there are many other documents to discuss how to sort it very detailed. If you download this example source code, it is included inside. Finally, I moved on the form of the link button (LinkButton) on the form. Here I perform any required deletion and update operations by looping data items in DataGrid. Private void btnupdate_click (Object sender, system.eventargs e) {// loop processing Each data item. Foreach (DataGridItem di in dgcontacts.Items) {/ confident is the data item instead of top or page. IF (di. ItemType == ListItemType.Item || Di.ItemType == ListItemType.AlternatingItem) {// Get the current line after the update or delete operation is executed. DataRow DR = _dscontacts.tables ["Contact"]. RowsFind (DGContacts.DataKeys [di.itemindex]); // Check if you need to delete a row. IF ((CheckBox) Di.FindControl ("chkDelete")). Checked) {_dscontacts.tables ["Contact"]. Rows.Remove (DR) ; // Remove the specified line} else {// update data line. DR ["email"] = ((textbox) Di.FindControl ("email"))))))))). Text; DR ["firstname"] = ((TextBox) Di .FindControl ("first")). Text; DR ["lastname"] = ((TextBox) Di.FindControl ("Last")). TEXT;}}} // Save it if there is change. IF (_dscontacts. Haschanges ()) {saveContacts ();} bindcontacts (); // Binds} I can easily find each DataGridItem in the control through the control of the control. There are a variety of ways to implement it, I am sure you can find a better way to complete this task. As you can see, editing the entire data grid once is very simple.