Hot questions about the DataGrid Web control

Source: Internet
Author: User
Tags define definition eval implement
Datagrid|web| Control | issues
Display a drop-down List in edit mode
Here is a common requirement for users-a drop-down List in which a row is in edit state. For example, a DataGrid may display a list of books, including the category of each book, when a user edits a book record, may want to assign a different category to the book, ideally, they can choose from the Drop-down list of possible category values, such as "novel", "Biological "or" Bibliography ".
Displaying a drop-down list requires a template column to be set in the DataGrid. Typically, an item template contains a control, such as a data-bound label control, that displays the current value of a field in a record. Then, add a Drop-down list to the Edit item template, in VS, you can add a template column through the DataGrid's Property Builder and remove the default TextBox control from the Edit item template through edit template, and drag into a Drop-down List control, you can also add template columns in HTML view.
There are two more tasks after you create a template column that contains Drop-down list: First, to produce a list of the preselected items in the secondary set list. For example, if the category of this book is set to "novel", when the Drop-down list is displayed, it is often necessary that "fiction" be pre-selected (a preselection item is not an issue to be discussed in all cases).
There are several ways to generate a drop-down list. The following examples illustrate three ways of using static items, using the collection of records in a dataset, and using DataReader to read information directly from the database.
L static items: displaying static items in the Drop-down list does not require binding data to the control. You can simply define items in the control's item collection. In VS, you can edit items in HTML view by triggering the item collection Editor from the Items property in the Properties window.
The following is a complete definition of a static list of categories that are displayed in a display mode under the Template column and in edit mode. The item template contains a Label control whose Text property is set to the genre field of the current record, and the static item definition in the Edit item template is highlighted.
<asp:templatecolumn headertext= "Genre" >
<ItemTemplate>
<asp:label id=label4 runat= "Server"
Text= ' <%# DataBinder.Eval (Container, "dataitem.genre")%> ' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist id= "DropDownList2" runat= "Server" width= "172px" >
<asp:listitem value= "Fiction" >fiction</asp:ListItem>
<asp:listitem value= "biography" >biography</asp:ListItem>
<asp:listitem value= "Reference" >reference</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
L DataSet
If you want the data displayed in the Drop-down list to be in a dataset, you can use a generic data-binding approach. The following is its declaration syntax. The Drop-down list binds the table of categories in the DataSet DsBooks1. Data binding settings are highlighted.
<asp:templatecolumn headertext= "Genre (DataSet)" >
<ItemTemplate>
<asp:label id=label3 runat= "Server"
Text= ' <%# DataBinder.Eval (Container, "dataitem.genre")%> ' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist id=dropdownlist4 runat= "Server"
Datasource= "<%# DsBooks1%>" datamember= "genre"
datatextfield= "Genre" datavaluefield= "genre" width= "160px" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
L DataReader
You can also generate Drop-down List directly from the database. This approach is more complex but more efficient. The reason is to read the database only when it is needed.
A fairly easy way to implement this is to take advantage of Web Forms data-binding expressions. Although the most common is to call the DataBinder.Eval method in a data-binding expression, you can actually use any of the available public members on the page. The following example shows you how to create a function to create, populate, and return a DataTable object that the Drop-down list can bind to.
In this case, you need to be able to execute a data command that gets the recordset you need. For example, you might want to define a command and set its CommandText property to "SELECT * from genres". To simplify the example, suppose you already have a connection object and a Command object on the page.
Starts with a common function in the Create page. This function creates a DataTable object and defines the set of columns you need. It then opens the connection, executes the command command to return a DataReader, traverses the DataReader, copies the data to the DataTable, and, finally, returns the DataTable as the return value of the function.
The following shows how to implement. There is only one column ("Genre") in the DataTable returned in this example. Usually only one column is required to generate the Drop-down List. If you need to set its text and value to a different value, you need two columns.
Public DataTable getgenretable ()
{
DataTable dtgenre = new DataTable ();
if (application["genretable"] = = null)
{
DataRow Dr;
DataColumn dc = new DataColumn ("genre");
DTGENRE.COLUMNS.ADD (DC);
This.sqlConnection1.Open ();
System.Data.SqlClient.SqlDataReader Dreader =
This.sqlCommand1.ExecuteReader ();
while (Dreader. Read ())
{
Dr = Dtgenre.newrow ();
Dr[0] = dreader[0];
DTGENRE.ROWS.ADD (DR);
}
This.sqlConnection1.Close ();
}
Else
{
Dtgenre = (DataTable) application["genretable"];
}
return dtgenre;
}
Note: This function stores the DataTable it creates in the application state, because the DataTable here is like a static query table, so it is not necessary to reread it every time a non-peer is converted to edit mode, and Because the same DataTable can be used by multiple users, it can be saved in a global application state rather than in a different session state depending on the user.
The following is a statement from the template column, which you will find very similar to the syntax of the table in the binding dataset, the only real difference being that the binding of the data source calls your own function. A slight disadvantage of this technique is that there is not much design-type service in VS. Because it is bound by a code definition DataTable, VS does not provide any way to set the DataMember, DataTextField, and DataValueField properties, you need to determine these properties yourself, Make them match the names of the members you created in your code.
<asp:templatecolumn headertext= "Genre (database)" >
<ItemTemplate>
<asp:label id=label1 runat= "Server"
Text= ' <%# DataBinder.Eval (Container, "dataitem.genre")%> ' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist id=dropdownlist1 runat= "Server"
Datasource= "<%# getgenretable ()%>"
datamember= "Genre"
datatextfield= "Genre"
datavaluefield= "Genre"
Width= "120px" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
(unfinished)

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.