Datagrid| created in the forum I have seen many of the same or similar questions: How do I place check boxes, text boxes, and so on in every row of my DataGrid? How do I update their values? The answer is fairly simple, and in this article I'll show you how to do it.
As we all know, the DataGrid is a very powerful tool. In my experience, in more than 90% of the time, the DataGrid is used to display data, and it is possible to edit a row of data at once. At some point, you might want to edit multiple lines or even all the data at once. A practical example is the application that sells items on the web, where customers may change one or more items in their baskets at one time, and click the check box to remove items they don't want.
Conception
In this example, I wrote a simple webform to manage the list of contacts stored in XML. This requirement is very simple: the ability to add new contacts and edit/delete existing contacts. Users can modify or delete more than one contact at a time, and I also allow the user to sort the data grid by their chosen column.
My example is written in C #. If you prefer the VB version of the code, there are two formats of code in the download file.
Contacts.xml
The XML data file in this example is very simple and intuitive. Because it's very simple, I didn't create a plan.
<?xml version= "1.0" standalone= "yes"?>
<Contacts>
<Contact>
<Email>myaddress@mycompany.com</Email>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Contact>
<Contact>
<Email>youraddress@yourcompany.com</Email>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
</Contact>
</Contacts>
Contactlist.aspx
Setting WebForm is very simple. I put a new DataGrid into my form and set it to 4 columns, and the first column contains a check box to delete the contact. You'll notice that the main job I'm doing here is to create each column in the form of a template column (TemplateColumn). This allows me to place text boxes and check box objects into data Templates (ItemTemplate). This is a technique for displaying something other than text in each row of the grid. In addition, you'll notice that I use FooterTemplate to make new contacts simple and intuitive.
I also included a link button (LinkButton) to save the user's modifications and deletions. However, it is not used to add new contacts. The action to add a new contact is done by the link button (LinkButton) in the footer template for the last column.
<asp:datagrid id= "dgcontacts" runat= "Server" showfooter= "true" allowsorting= "true" Forefont color= "BLACK" Gridlines= "None" cellpadding= "2" Backfont color= "Lightgoldenrodyellow" borderwidth= "1px" Borderfont color= "Tan" Width = "499px" autogeneratecolumns= "False" datakeyfield= "Email" >
<selecteditemstyle forefont color= "Ghostwhite" Backfont color= "Darkslateblue" ></SelectedItemStyle>
<alternatingitemstyle backfont color= "Palegoldenrod" ></AlternatingItemStyle>
<footerstyle backfont color= "Tan" ></FooterStyle>
<Columns>
<asp:templatecolumn sortexpression= "FirstName" headertext= "Name" >
<ItemTemplate>
<asp:textbox id=first runat= "Server" width= "109px" text= ' <%# DataBinder.Eval (Container, "dataitem.firstname")% > ' >
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:textbox id= "Newfirst" runat= "Server" width= "109px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:templatecolumn sortexpression= "LastName" headertext= "Last Name" >
<ItemTemplate>
<asp:textbox id=last runat= "Server" width= "109px" text= ' <%# DataBinder.Eval (Container, "dataitem.lastname")% > ' >
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:textbox id= "Newlast" runat= "Server" width= "109px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:templatecolumn sortexpression= "Email" headertext= "Email" >
<ItemTemplate>
<asp:textbox id=email runat= "server" text= ' <%# DataBinder.Eval (Container, "Dataitem.email")%> ' >
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:textbox id= "Newemail" runat= "Server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Delete Contact" >
<itemstyle horizontalalign= "Center" ></ItemStyle>
<ItemTemplate>
<asp:checkbox runat= "Server" id= "Chkdelete" ></asp:CheckBox>
</ItemTemplate>
<footerstyle horizontalalign= "Center" ></FooterStyle>
<FooterTemplate>
<asp:linkbutton runat= "Server" text= "Add" commandname= "Add" id= "Linkbutton1" name= "Linkbutton1" ></asp: Linkbutton>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</