Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

240

Sunday Morning

Return dataview1

Catch myException as Exception

Message.Text = (“Exception: “ + myException.ToString())

End Try

End Function

Next, we bind the DataGrid control with the filtered DataView object. This is done with the final two statements of the BindData() function:

titleGrid.DataSource = TitlesDataView titleGrid.DataBind()

First, you set the DataSource property of the titleGrid to the Default View of the TitlesDataView, and then you execute the titleGrid.DataBind() method to populate the DataGrid object. Figure 23-3 shows the results of your work.

Figure 23-3 Example of using the DataGrid control for master/detail relationships

REVIEW

At this point you should be able to implement master/detail user interfaces in your applications. While the use of the DataGrid control has made your job much easier, the core part of the work continues to be understanding the use of ADO.NET objects in getting and filtering data.

Session 23—Using the DataGrid Control with Bound Data

241

QUIZ YOURSELF

1.How do you control the formatted display of values in a BoundColumn? (See “Formatting the output of a DataGrid control.”)

2.How can you control the ordering of values into columns of a DataGrid? (See Master/Detail Relationships with the DataGrid Control.”)

3.Is a master/detail user interface more useful for a many-to-many relationship or a one-to-many relationship? (See “Master/Detail Relationships with the DataGrid Control.”)

S E S S I O N

24

Beating the CRUD out of the

DataGrid Control

Session Checklist

Using the DataGrid control as a user interface for modifying data

Using validation controls with template columns for data validation

Sorting the columns of a DataGrid control

In Session 23, we illustrated how you can use the DataGrid control to support the display of data. However, just showing the data is useless if you can’t provide updates or deletes. In this session we will build upon the examples used in Session 23 to illustrate how the DataGrid control can be used to update data. The DataGrid control comes with a

whole host of built-in functionality that can be exposed to provide a highly customizable approach to building user interfaces to your database.

This session assumes you have SQL Server Installed and have access to the Pubs database.

Note

Updating Your Data

The DataGrid control provides the capability to support editing of bound data by using the EditCommandColumn. The EditCommandColumn handles the automatic generation of “Edit”, “OK”, and “Cancel” hyperlinks or images to facilitate the user interface elements of editing a DataGrid control. When the “Edit” hyperlink or alternatively an image is selected, the EditCommandColumn control will replace a DataGrid control read-only cell with an editable textbox. Figure 24-1 illustrates the resulting output of using the EditCommandColumn to build the “Edit” hyperlink.

244

Sunday Morning

Figure 24-1 Output of using the EditCommandColumn for editing a DataGrid control

The property value that generates the EditCommandColumn is shown in the following code:

<asp:EditCommandColumn

EditText=”Edit”

CancelText=”Cancel”

UpdateText=”OK” > </asp:EditCommandColumn>

Figure 24-2 illustrates the results of selecting the “Edit” hyperlink to edit a selected column.

Figure 24-2 Example of a data row being edited

Session 24—Beating the CRUD out of the DataGrid Control

245

The DataGrid control will automatically display the appropriate hyperlinks — an “Edit” hyperlink when in standard mode, a “Cancel” and “Submit” hyperlink when in Edit mode. While the EditCommandColumn handles the generation of the GUI, you still have to provide all of the code to actually perform the edit, cancel and update functions. You attach the code modules to call when each of these events happen through the OnEditCommand, OnUpdateCommand, and OnCancel properties of the DataGrid control. For each of these properties you implement an appropriate function to handle the mechanics of the operation. Listing 24-1 illustrates the full HTML required to generate the DataGrid control and

EditCommandColumn as discussed.

Listing 24-1 HTML required to generate the DataGrid and EditCommandColumn

<ASP:DATAGRID ID=”titleGrid” RUNAT=”SERVER” FORECOLOR=”Black” AUTOGENERATECOLUMNS=”false” DATAKEYFIELD=”title_id”

ONPAGEINDEXCHANGED=”OnPageIndexChanged”

ONEDITCOMMAND=”OnEdit”

ONCANCELCOMMAND=”OnCancel”

ONUPDATECOMMAND=”OnUpdate”

ONDELETECOMMAND=”OnDelete”

ONSORTCOMMAND=”OnSorted”

ALLOWSORTING=”True”

ALLOWPAGING=”True”

PAGESIZE=”5” PAGERSTYLE-MODE=”NextPrev” PAGERSTYLE-HORIZONTALALIGN=”Center”>

<ALTERNATINGITEMSTYLE BACKCOLOR=”Gainsboro” /> <FOOTERSTYLE BACKCOLOR=”Silver” FORECOLOR=”White” /> <ITEMSTYLE BACKCOLOR=”White” />

<HEADERSTYLE BACKCOLOR=”Navy” FORECOLOR=”White” FONT-BOLD=”True” />

<COLUMNS>

<ASP:BOUNDCOLUMN HEADERTEXT=”Title” DATAFIELD=”Title” SORTEXPRESSION=”Title”>

</ASP:BOUNDCOLUMN>

<ASP:TEMPLATECOLUMN HEADERTEXT=”Unit Price” SORTEXPRESSION=”Price”>

<ITEMTEMPLATE>

<ASP:LABEL id=”Label2” RUNAT=”server”

TEXT=’<%# String.Format(“{0:C}”,

Container.DataItem(“price”))%>’

WIDTH=”50”

AUTOSIZE=”True”>

</ASP:LABEL>

</ITEMTEMPLATE>

<EDITITEMTEMPLATE> <ASP:TEXTBOX id=”editprice”

RUNAT=”Server”

TEXT=’<%# Container.DataItem(“price”)%>’

Continued