Friday, November 4, 2011

Friday 11.4.11

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
'get the title of the courtesy for the item that's being created
Dim title As String = CStr(DataBinder.Eval(e.Row.DataItem, "TitleOfCourtesy"))
'if the title of courtesy is "Ms.", "Mrs." or "Mr." change the item's colors
If title = "Ms." OrElse title = "Mrs." Then
e.Row.BackColor = System.Drawing.Color.LightPink
e.Row.ForeColor = System.Drawing.Color.Maroon
ElseIf title = "Mr." Then
e.Row.BackColor = System.Drawing.Color.LightCyan
e.Row.ForeColor = System.Drawing.Color.DarkBlue
End If
End If
End Sub



Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim index As Integer = GridView1.SelectedIndex
'you can retrieve teh key field from the SelectedDataKey property
Dim ID As Integer = CInt(GridView1.SelectedDataKey.Values("EmployeeID"))
'you can retrieve other data directly from teh Cells colelction
'as long as you know the column offset
Dim firstName As String = GridView1.SelectedRow.Cells(2).Text
Dim lastName As String = GridView1.SelectedRow.Cells(3).Text

lblRegionCaption.Text = "Regions that " & firstName & " " & lastName & " (employee " & ID.ToString() & ") is responsible For: "

Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Northwind").ConnectionString
Dim con As New SqlConnection(connectionString)

Dim ParamCommand As SqlCommand = New SqlCommand()
ParamCommand.Connection = con
ParamCommand.CommandText = "SELECT Employees.EmployeeID, Territories.TerritoryID, Territories.TerritoryDescription FROM Employees INNER JOIN EmployeeTerritories ON Employees.EmployeeID = EmployeeTerritories.EmployeeID INNER JOIN Territories ON EmployeeTerritories.TerritoryID = Territories.TerritoryID WHERE (Employees.EmployeeID = @EmployeeID)"
ParamCommand.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = GridView1.SelectedDataKey.Values("EmployeeID")

con.Open()
Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(ParamCommand)
Dim ds As New DataSet()
dataAdapter.Fill(ds, "Territories")
GridView2.DataSource = ds
GridView2.DataBind()
End Sub

No comments: