Specifying Columns Explicitly


In DataGrid.aspx, we take advantage of the DataGrid control’s ability to automatically render the structure of the data source in the user interface. The column headers are the names of the columns, and the column order replicates that of the DataColumn objects in the DataTable. There is a performance cost associated with the reflection required to produce this automatic rendering.

Another drawback with the auto-generated columns is that in production databases, column names are rarely names that would mean much to the user, and the query might include columns that are intended for use by the application but should not be used by or shown to the user. The DataGrid control allows us to override this behavior by setting the AutoGenerateColumns property to false and providing a collection of BoundColumn definitions within a Columns element declaratively in the page. Only the specified fields from the bound data are displayed instead. Code Listing 3-8 is a modified version of the previous DataGrid example in which AutoGenerateColumns is set to false and we provide explicit header names for the columns. In the coming examples, we will continue to modify this listing of states and their time zones to illustrate ways of using and controlling the DataGrid.

Code Listing 3-8: DataGridColumns.aspx

start example
 <%@Import namespace="System.Data" %>
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
datagrid.DataSource = GetData();
DataBind();
}

DataTable GetData() {
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("TheID", typeof(Int32)));
data.Columns.Add(new DataColumn("Name", typeof(string)));
data.Columns.Add(new DataColumn("TimeZone", typeof(string)));

DataRow dr;
dr = data.NewRow();
dr[0] = 1; dr[1] = "Washington"; dr[2] = "Pacific";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 2; dr[1] = "Utah"; dr[2] = "Mountain";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 3; dr[1] = "Wisconsin"; dr[2] = "Central";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 4; dr[1] = "New York"; dr[2] = "Eastern";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 5; dr[1] = "Florida"; dr[2] = "Eastern";
data.Rows.Add(dr);

return data;
}
</script>
<form runat="server">
<asp:DataGrid runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn HeaderText="ID" DataField="TheID" />
<asp:BoundColumn HeaderText="Name" DataField="Name" />
<asp:BoundColumn HeaderText="Time Zone"
DataField="TimeZone" />
</Columns>
</asp:DataGrid>
</form>
end example

start sidebar
Combining Data-Bound and Static Items

When a control is data-bound, the items collection is cleared. Any statically declared items, items from an earlier data-binding, or items restored from ViewState are lost—not always an ideal situation. Sometimes the goal is to combine items from a back-end database with statically known data or with user-provided data. The DataGridItemCollection does not permit inserts or additions, so you combine sets of data before the data is bound to the control. Select the data from the database and then add the user- defined data. The DataBind operation will bind the augmented set of data to the control, reflecting the changes appropriately.

end sidebar




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net