Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString, strSQL, strFields As String
Dim dtAdapter As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
DropDownRawMaterialType()
GridViewRawMaterial()
End If
End Sub
Public Sub ConnectDB()
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
objConn = New SqlConnection(strConnString)
End Sub
Private Sub GridViewRawMaterial()
'ResetAll()
ConnectDB()
strSQL = "SELECT RM.RM_RKEY, RMT.RMT_NAME, RM.RM_NAME FROM RawMaterial AS RM
INNER JOIN RawMaterialType AS RMT ON RMT.RMT_RKEY = RM.RMT_RKEY
ORDER BY RMT.RMT_NAME, RM.RM_NAME"
dtAdapter = New SqlDataAdapter(strSQL, objConn)
ds = New DataSet()
objConn.Open()
dtAdapter.Fill(ds)
objConn.Close()
gvLoadRM.DataSource = ds
gvLoadRM.DataBind()
End Sub
Function DropDownRawMaterialType() As DataTable
ConnectDB()
strSQL = "SELECT RMT_RKEY, RMT_NAME FROM RawMaterialType"
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dt = New DataTable
objConn.Open()
dtAdapter.Fill(dt)
objConn.Close()
ddlLoadRMT.DataSource = dt
ddlLoadRMT.DataTextField = "RMT_NAME"
ddlLoadRMT.DataValueField = "RMT_RKEY"
ddlLoadRMT.DataBind()
ddlLoadRMT.Items.Insert(0, New ListItem("กรุณาเลือกประเภทวัตถุดิบ", "0"))
ddlLoadRMT.SelectedIndex = ddlLoadRMT.Items.IndexOf(ddlLoadRMT.Items.FindByValue("0"))
Return dt
End Function
Private Sub gvLoadRM_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvLoadRM.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlRMTName As DropDownList = CType(e.Row.FindControl("ddlEditRMTName"), DropDownList)
If Not IsNothing(ddlRMTName) Then
ddlRMTName.DataSource = DropDownRawMaterialType()
ddlRMTName.DataTextField = "RMT_NAME"
ddlRMTName.DataValueField = "RMT_RKEY"
ddlRMTName.DataBind()
Dim use As String = e.Row.DataItem("RMT_NAME")
ddlRMTName.SelectedValue = use
Label4.Text = use
End If
End If
End If
End Sub
Private Sub gvLoadRM_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gvLoadRM.PageIndexChanging
gvLoadRM.PageIndex = e.NewPageIndex
GridViewRawMaterial()
End Sub
Private Sub gvLoadRM_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gvLoadRM.RowEditing
gvLoadRM.EditIndex = e.NewEditIndex
GridViewRawMaterial()
End Sub
Private Sub gvLoadRM_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles gvLoadRM.RowCancelingEdit
gvLoadRM.EditIndex = -1
GridViewRawMaterial()
End Sub
End Class