前几天做的项目里用到了,顺便写下来[emot]pig[/emot]
要求输入一条查询语句,在DataGrid里增加一条数据
数据可以进行单行选择和删除操作,可以根据条件判断进行单列编辑
为实现此要求,以Session为数据源进行DataGrid数据绑定
DataTable与Session中的DataTable数据同步
下面是我写的简单的数据绑顶示例:

Private Function GridBind()Function GridBind() As Boolean
On Error GoTo ERR_FLAG
Dim strSql As String = ""
Dim dSet As New DataSet
Dim dTbl As New DataTable
GridBind = False

grdChkStr = Right(Trim(txt_code.Text), 8)

strSql &= " Select G02発注.C_GYOU AS C_GYOU, "
strSql &= " CD業者地域.CodeName AS CodeName, "
strSql &= " G02発注.C_KJ AS C_KJ , "
strSql &= " G02発注.C_ORDER AS C_ORDER, "
strSql &= " (CONVERT(FLOAT,G02発注.M_ORDER)+CONVERT(FLOAT,G02発注.N_YOBI1)) AS Order_Money, "
strSql &= " CONVERT(CHAR, G02発注.D_ORDER, 111) AS D_ORDER, "
strSql &= " CONVERT(CHAR, G02発注.D_JYURYOU, 111)AS D_JYURYOU "
strSql &= " From G02発注 "
strSql &= " LEFT OUTER JOIN CD業者地域 "
strSql &= " ON G02発注.C_GYOU = CD業者地域.Code "
strSql &= " LEFT OUTER JOIN J29契約 "
strSql &= " ON G02発注.C_KJ = J29契約.C_KJ "
strSql &= " Where 1 = 1 "
strSql &= " AND G02発注.C_ORDER = '" & grdChkStr & "' "
strSql &= " AND G02発注.C_NOUHIN = '1' "
strSql &= " "

dSet = Me.DBCommon.ADONETGetDataSetfromSQL(strSql)
If dSet.Tables(0).Rows.Count = 0 Then
GoTo ERR_FLAG
End If
dTbl = addData(dSet)
Me.grd_list.DataSource = dTbl
Me.grd_list.DataBind()

Dim rdoChk As New HtmlControls.HtmlInputRadioButton
rdoChk = CType(Me.grd_list.Items(0).FindControl("rdoChoise"), HtmlInputRadioButton)
rdoChk.Checked = True

dSet.Clear()
GridBind = True
Exit Function

ERR_FLAG:
If Not dSet Is Nothing Then
dSet.Clear()
End If
GridBind = False
Exit Function
End Function


Private Function addData()Function addData(ByVal dSet As DataSet) As DataTable
On Error GoTo ERR_FLAG

Dim dTbl As New DataTable
Dim dRow As DataRow
Dim sesTemp As New clsSession

sesTemp.GetSession("WG52_DT", dTbl)
If dTbl Is Nothing Then
dTbl = setColumns()
End If
dRow = dTbl.NewRow()
If Not setRow(dSet, dRow) Then
GoTo ERR_FLAG
End If
dTbl.Rows.InsertAt(dRow, 0)
dTbl.AcceptChanges()
sesTemp.SetSession("WG52_DT", dTbl)
addData = dTbl
Exit Function
ERR_FLAG:
addData = Nothing
Exit Function
End Function


Private Function setColumns()Function setColumns() As DataTable
Dim dTbl As DataTable
dTbl = New DataTable

With dTbl.Columns
.Add(New DataColumn("C_GYOU", GetType(String)))
.Add(New DataColumn("CodeName", GetType(String)))
.Add(New DataColumn("C_KJ", GetType(String)))
.Add(New DataColumn("C_ORDER", GetType(String)))
.Add(New DataColumn("N_KJ", GetType(String)))
.Add(New DataColumn("Order_Money", GetType(String)))
.Add(New DataColumn("D_ORDER", GetType(String)))
.Add(New DataColumn("D_JYURYOU", GetType(String)))
End With

setColumns = dTbl
End Function


Function setRow()Function setRow(ByVal dSet As DataSet, ByRef dr As DataRow) As Boolean
On Error GoTo ERR_FLAG

setRow = False
dr("C_GYOU") = NullToString(dSet.Tables(0).Rows(0).Item("C_GYOU"))
dr("CodeName") = NullToString(dSet.Tables(0).Rows(0).Item("CodeName"))
dr("C_KJ") = NullToString(dSet.Tables(0).Rows(0).Item("C_KJ"))
dr("C_ORDER") = NullToString(dSet.Tables(0).Rows(0).Item("C_ORDER"))
dr("N_KJ") = NullToString(dSet.Tables(0).Rows(0).Item("N_KJ"))
dr("Order_Money") = Format(NullToString(dSet.Tables(0).Rows(0).Item("Order_Money")), "###,###,##0")
dr("D_ORDER") = NullToString(dSet.Tables(0).Rows(0).Item("D_ORDER"))
dr("D_JYURYOU") = NullToString(dSet.Tables(0).Rows(0).Item("D_JYURYOU"))
setRow = True

Exit Function
ERR_FLAG:
setRow = False
End Function

Back