All,
The long anticipated answer lies below.
Thearon
This is the HTML code for your DataGrid:
<asp:DataGrid id=grdAuthors
style="Z-INDEX: 101; LEFT: 12px; POSITION: absolute; TOP: 12px"
runat="server"
AlternatingItemStyle-BackColor=WhiteSmoke
AutoGenerateColumns=false
BackColor=White
CellPadding=3
DataKeyField="title_id"
Font-Name=Verdana
Font-Size=8pt
GridLines=None
HeaderStyle-BackColor=Maroon
HeaderStyle-Font-Bold=true
HeaderStyle-ForeColor=White
OnEditCommand="EditGridData"
OnCancelCommand="CancelGridData"
OnUpdateCommand="UpdateGridData"
AllowSorting="true">
<Columns>
<asp:EditCommandColumn
EditText="Edit Row"
CancelText="Cancel Edit"
UpdateText="Update Row"
ItemStyle-Wrap="False"/>
<asp:BoundColumn
DataField="title_id"
Visible="False"/>
<asp:BoundColumn
DataField="au_lname"
HeaderText="Last Name"
ReadOnly="True"
SortExpression="au_lname"
ItemStyle-Wrap="False"/>
<asp:BoundColumn
DataField="au_fname"
HeaderText="First Name"
ReadOnly="True"
SortExpression="au_fname"
ItemStyle-Wrap="False"/>
<asp:TemplateColumn
HeaderText="Title"
ItemStyle-Wrap="False"
SortExpression="title">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# DataBinder.Eval (Container.DataItem, "title") %>' ID="Label1"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server"
ID="edit_title"
Font-Name="Verdana"
Font-Size="8pt"
Width="400"
Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Price"
HeaderStyle-Font-Bold="True"
ItemStyle-HorizontalAlign=Right
SortExpression="price">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}")%>' ID="Label2"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server"
ID="edit_price"
Font-Name="Verdana"
Font-Size="8pt"
Width="50"
Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
This is the
VB.Net code for your code behind file:
'Declare a Connection object that is global in scope
Dim objConnection As SqlConnection
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialize the Connection object
objConnection = New SqlConnection( _
"Server=localhost;Database=Pubs;User ID=sa;Password=P5662ab#;")
'Only bind the data the first time the page is built
'Subsequent post backs will be to sort the data in the grid
If Not (IsPostBack) Then
'Set a session variable with the sort column
Session("SortColumn") = "au_lname"
BindGrid(Session("SortColumn"))
End If
End Sub
Sub BindGrid(ByVal strSortField As String)
'Declare objects
Dim objDataSet As DataSet
Dim objDataAdapter As SqlDataAdapter
'Set the SQL string
objDataAdapter = New SqlDataAdapter( _
"SELECT au_lname, au_fname, titles.title_id, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY " & strSortField, _
objConnection)
'Initialize the DataSet object and fill it
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Authors")
'Bind the DataGrid
grdAuthors.DataSource = objDataSet
grdAuthors.DataBind()
'Cleanup
objDataAdapter = Nothing
objDataSet = Nothing
End Sub
Private Sub grdAuthors_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles grdAuthors.SortCommand
'Save the selected sort column in the session variable
Session("SortColumn") = e.SortExpression
'Bind the data grid using the sort column requested
BindGrid(Session("SortColumn"))
End Sub
Sub EditGridData(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
grdAuthors.EditItemIndex = CInt(E.Item.ItemIndex)
'Bind the DataGrid using the saved sort column
BindGrid(Session("SortColumn"))
End Sub
Sub CancelGridData(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
grdAuthors.EditItemIndex = -1
'Bind the DataGrid using the saved sort column
BindGrid(Session("SortColumn"))
End Sub
Sub UpdateGridData(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim objCommand As SqlCommand
Dim objTextBox As TextBox
Dim strSQL As String = "UPDATE titles " & _
"SET title = @Title, price = @Price " & _
"WHERE title_id = @ID"
objCommand = New SqlCommand(strSQL, objConnection)
objCommand.Parameters.Add(New SqlParameter("@ID", _
SqlDbType.VarChar, 6))
objCommand.Parameters.Add(New SqlParameter("@Title", _
SqlDbType.VarChar, 80))
objCommand.Parameters.Add(New SqlParameter("@Price", _
SqlDbType.Money, 8))
objCommand.Parameters("@Id").Value = _
grdAuthors.DataKeys(CInt(E.Item.ItemIndex))
objTextBox = E.Item.FindControl("edit_title")
objCommand.Parameters("@Title").Value = objTextBox.Text
objTextBox = E.Item.FindControl("edit_price")
If Left(objTextBox.Text, 1) = "$" Then
objTextBox.Text = _
Right(objTextBox.Text, Len(objTextBox.Text) - 1)
End If
objCommand.Parameters("@Price").Value = objTextBox.Text
objCommand.Connection.Open()
objCommand.ExecuteNonQuery()
grdAuthors.EditItemIndex = -1
objCommand.Connection.Close()
'Bind the DataGrid using the saved sort column
BindGrid(Session("SortColumn"))
objCommand = Nothing
objTextBox = Nothing
End Sub
Notice the SortExpression property in the BoundColumn's and TemplateColumn's in the HTML code. The key for the code behind file lies in using a Session variable to store the sort field that the user has chosen. This sort field is then used in the SQL SELECT statement when retreiving the data from the database and in the calls to the BindGrid procedure.