<asp:DataGrid id="DataGrid1" runat="server" CssClass="grid" AutoGenerateColumns="False"> <Columns> <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" /> <asp:BoundColumn DataField="OrderID" ReadOnly="True" HeaderText="Order ID" /> <asp:BoundColumn DataField="ShipName" HeaderText="Ship to" ReadOnly="True" /> <asp:BoundColumn DataField="ShipCountry" HeaderText="Country" ReadOnly="True" /> <asp:TemplateColumn HeaderText="Ship Method"> <ItemTemplate> <%#Container.DataItem("ShipVia")%> </ItemTemplate> <EditItemTemplate> <asp:DropDownList runat="server" ID="Dropdownlist1"/> </EditItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> |
Sub BindGrid() Dim SQL As String = "SELECT OrderID, ShipName, ShipCountry, ShipVia FROM Orders" Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr) Dim DS As New DataSet DA.Fill(DS, "Orders") DataGrid1.DataSource = DS.Tables("Orders").DefaultView DataGrid1.DataBind() End Sub |
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _ Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.EditItem Then Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView) Dim CurrentShip As String = DRV("ShipVia") Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList) Dim SQL As String = "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID" Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr) Dim DS As New DataSet Dim item As ListItem DA.Fill(DS, "Shippers") DDL.DataSource = DS.Tables("Shippers").DefaultView DDL.DataTextField = "CompanyName" DDL.DataValueField = "ShipperID" DDL.DataBind() item = DDL.Items.FindByValue(CurrentShip) If Not item Is Nothing Then item.Selected = True End If End Sub |
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _ Handles DataGrid1.UpdateCommand Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList) Dim NewShip As Integer = DDL.SelectedValue Dim OrderID As Integer = Int32.Parse(e.Item.Cells(1).Text) Dim SQL As String = "UPDATE Orders SET ShipVia=@Ship WHERE OrderID=@ID" Dim Conn As SqlConnection = New SqlConnection(ConnStr) Dim Cmd As New SqlCommand(SQL, Conn) Cmd.Parameters.Add(New SqlParameter("@Ship", NewShip)) Cmd.Parameters.Add(New SqlParameter("@ID", OrderID)) Conn.Open() Cmd.ExecuteNonQuery() Conn.Close() DataGrid1.EditItemIndex = -1 BindGrid() End Sub |
DataGridColumn 属性 FooterStyle (TableItemStyle) FooterText(字符串) HeaderImageUrl(字符串) HeaderStyle (TableItemStyle) HeaderText(字符串) ItemStyle (TableItemStyle) SortExpression(字符串) Visible(布尔值) DataGridColumn 方法 Initialize InitializeCell LoadViewState OnColumnChanged SaveViewState TrackViewState |
Namespace MyCustomColumn Public Class DropDownColumn Inherits DataGridColumn Public DataSource As ICollection Public DataField As String Public DataTextField As String Public DataValueField As String End Class End Namespace |
Public Overrides Sub InitializeCell(ByVal cell As TableCell, _ ByVal columnIndex As Integer, _ ByVal itemType As ListItemType) MyBase.InitializeCell(cell, columnIndex, itemType) Select Case itemType Case ListItemType.Header cell.Text = HeaderText Case ListItemType.Item, ListItemType.AlternatingItem AddHandler cell.DataBinding, AddressOf ItemDataBinding Case ListItemType.EditItem AddHandler cell.DataBinding, AddressOf EditItemDataBinding Dim DDL As New DropDownList cell.Controls.Add(DDL) End Select End Sub |
Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As EventArgs) Dim cell As TableCell = CType(sender, TableCell) Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem) Try cell.Text = DGI.DataItem(DataField) Catch RangeEx As IndexOutOfRangeException Throw New Exception("Specified DataField was not found.") Catch OtherEx As Exception Throw New Exception(OtherEx.InnerException.ToString) End Try End Sub |
Private Sub EditItemDataBinding(ByVal sender As Object, _ ByVal e As EventArgs) Dim cell As TableCell = CType(sender, TableCell) Dim DDL As DropDownList = _ CType(cell.Controls(0), DropDownList) Dim DataSourceItem As Object Dim item As ListItem Dim DGI As DataGridItem '首先添加一个空选项 DDL.Items.Add(New ListItem("")) For Each DataSourceItem In DataSource Select Case DataSourceItem.GetType.ToString Case "System.String" '应用到 ArrayList 示例 item = New ListItem(DataSourceItem, DataSourceItem) DDL.Items.Add(item) Case "System.Data.DataRowView" Dim DRV As DataRowView = _ CType(DataSourceItem, DataRowView) item = New_ ListItem(DRV(DataTextField), DRV(DataValueField)) DDL.Items.Add(item) Case Else Throw New Exception("Invalid DataSource type.") End Select Next Try DGI = CType(cell.NamingContainer, DataGridItem) item = DDL.Items.FindByValue(DGI.DataItem(DataField)) Catch RangeEx As IndexOutOfRangeException Throw New Exception("Specified DataField was not found.") Catch OtherEx As Exception Throw New Exception(OtherEx.InnerException.ToString) End Try If Not item Is Nothing Then item.Selected = True End Sub |
<%@ Register TagPrefix="dgg" Namespace="MyCustomColumn" Assembly="MyCustomColumn" %> |
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="UseCustomColumn.WebForm1" Trace="False" Debug="True"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>WebForm1</title> <LINK rel="stylesheet" type="text/css" href="Styles.css"> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" runat="server" CssClass="grid" AutoGenerateColumns="False"> <Columns> <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" /> <asp:BoundColumn DataField="OrderID" ReadOnly="True" HeaderText="Order ID"/> <asp:BoundColumn DataField="ShipName" HeaderText="Ship to" ReadOnly="True"/> <asp:BoundColumn DataField="ShipCountry" HeaderText="Country" ReadOnly="True"/> <dgg:DropDownColumn DataField="ShipVia" HeaderText="Ship Method" /> </Columns> </asp:DataGrid> </form> </body> </HTML> |
Dim DDC As MyCustomColumn.DropDownColumn DDC = CType(DataGrid1.Columns(4), MyCustomColumn.DropDownColumn) Dim AL As New ArrayList AL.Add("Shipping Company A") AL.Add("Shipping Company B") AL.Add("Shipping Company C") DDC.DataSource = AL |
<dgg:DropDownColumn DataField="ShipVia" DataTextField="CompanyName" DataValueField="ShipperID" HeaderText="Ship Method" /> |
Dim SQL As String = "SELECT OrderID, ShipName, ShipCountry, ShipVia FROM Orders" Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr) Dim DS As New DataSet DA.Fill(DS, "Orders") 'Dim Cmd As SqlCommand = New SqlCommand(SQL, Conn) 'Conn.Open() 'DataGrid1.DataSource = _ Cmd.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataSource = DS.Tables("Orders").DefaultView SQL = "SELECT ShipperID, CompanyName " & _ "FROM Shippers ORDER BY ShipperID" DA.SelectCommand.CommandText = SQL DA.Fill(DS, "Shippers") DDC.DataSource = DS.Tables("Shippers").DefaultView DataGrid1.DataBind() |
Private Sub DataGrid1_UpdateCommand( ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _ Handles DataGrid1.UpdateCommand Dim CustomDDL As DropDownList = _ CType(e.Item.Cells(4).Controls(0), DropDownList) Dim NewShip As Integer = CustomDDL.SelectedValue Dim OrderID As Integer = Int32.Parse(e.Item.Cells(1).Text) Dim SQL As String = "UPDATE Orders SET ShipVia=@Ship WHERE OrderID=@ID" Dim Conn As SqlConnection = New SqlConnection(ConnStr) Dim Cmd As New SqlCommand(SQL, Conn) Cmd.Parameters.Add(New SqlParameter("@Ship", NewShip)) Cmd.Parameters.Add(New SqlParameter("@ID", OrderID)) Conn.Open() Cmd.ExecuteNonQuery() Conn.Close() DataGrid1.EditItemIndex = -1 BindGrid() End Sub |