| 
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ประมาณนี้ไหมครับ Code (VB.NET)
 
 sqlcon (1) 'ต่อ connectionstring
sb.remove(0,sb.legnth)
sb.append ("Select customerid,customername,customernickname From customer")
sql = sb.tostring 'นำค่ามาเก็บในตัวแปร string
dt = showdatasql (sql,"dtCustomer") ' นำค่าใน string มายัดใส่ใน datable
with datagridview
.datasource = dt 'โชว์ข้อมูลใน datagridview
end with
 
 ผิดพลาดประการใดขอ อภัย ด้วยนะครับ
 tee
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-07-21 16:02:43 | By :
                            lee_latee |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (DataGrid1.aspx.vb) 
 Imports System.Data
Imports System.Data.SqlClient
Partial Class DataGrid1
    Inherits System.Web.UI.Page
    Dim objConn As SqlConnection
    Dim objCmd As SqlCommand
    Dim strSQL As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strConnString As String
        strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
        objConn = New SqlConnection(strConnString)
        objConn.Open()
        If Not Page.IsPostBack() Then
            BindData()
        End If
    End Sub
    Protected Sub BindData()
        strSQL = "SELECT * FROM customer"
        Dim dtReader As SqlDataReader
        objCmd = New SqlCommand(strSQL, objConn)
        dtReader = objCmd.ExecuteReader()
        '*** BindData to DataGrid ***'
        myDataGrid.DataSource = dtReader
        myDataGrid.DataBind()
        dtReader.Close()
        dtReader = Nothing
    End Sub
    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        objConn.Close()
        objConn = Nothing
    End Sub
    Protected Sub myDataGrid_CancelCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles myDataGrid.CancelCommand
        myDataGrid.EditItemIndex = -1
        myDataGrid.ShowFooter = True
        BindData()
    End Sub
    Protected Sub myDataGrid_DeleteCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles myDataGrid.DeleteCommand
        strSQL = "DELETE FROM customer WHERE CustomerID = '" & myDataGrid.DataKeys.Item(e.Item.ItemIndex) & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()
        myDataGrid.EditItemIndex = -1
        BindData()
    End Sub
    Protected Sub myDataGrid_EditCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles myDataGrid.EditCommand
        myDataGrid.EditItemIndex = e.Item.ItemIndex
        myDataGrid.ShowFooter = False
        BindData()
    End Sub
    Protected Sub myDataGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles myDataGrid.ItemCommand
        If e.CommandName = "Add" Then
            '*** CustomerID ***'
            Dim txtCustomerID As TextBox = CType(e.Item.FindControl("txtAddCustomerID"), TextBox)
            '*** Name ***'
            Dim txtName As TextBox = CType(e.Item.FindControl("txtAddName"), TextBox)
            '*** Email ***'
            Dim txtEmail As TextBox = CType(e.Item.FindControl("txtAddEmail"), TextBox)
            '*** CountryCode ***'
            Dim txtCountryCode As TextBox = CType(e.Item.FindControl("txtAddCountryCode"), TextBox)
            '*** Budget ***'
            Dim txtBudget As TextBox = CType(e.Item.FindControl("txtAddBudget"), TextBox)
            '*** Used ***'
            Dim txtUsed As TextBox = CType(e.Item.FindControl("txtAddUsed"), TextBox)
            strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
            " VALUES ('" & txtCustomerID.Text & "','" & txtName.Text & "','" & txtEmail.Text & "' " & _
            " ,'" & txtCountryCode.Text & "','" & txtBudget.Text & "','" & txtUsed.Text & "') "
            objCmd = New SqlCommand(strSQL, objConn)
            objCmd.ExecuteNonQuery()
            BindData()
        End If
    End Sub
    Protected Sub myDataGrid_UpdateCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles myDataGrid.UpdateCommand
        '*** CustomerID ***'
        Dim txtCustomerID As TextBox = CType(e.Item.FindControl("txtEditCustomerID"), TextBox)
        '*** Email ***'
        Dim txtName As TextBox = CType(e.Item.FindControl("txtEditName"), TextBox)
        '*** Name ***'
        Dim txtEmail As TextBox = CType(e.Item.FindControl("txtEditEmail"), TextBox)
        '*** CountryCode ***'
        Dim txtCountryCode As TextBox = CType(e.Item.FindControl("txtEditCountryCode"), TextBox)
        '*** Budget ***'
        Dim txtBudget As TextBox = CType(e.Item.FindControl("txtEditBudget"), TextBox)
        '*** Used ***'
        Dim txtUsed As TextBox = CType(e.Item.FindControl("txtEditUsed"), TextBox)
        strSQL = "UPDATE customer SET CustomerID = '" & txtCustomerID.Text & "' " & _
        " ,Name = '" & txtName.Text & "' " & _
        " ,Email = '" & txtEmail.Text & "' " & _
        " ,CountryCode = '" & txtCountryCode.Text & "' " & _
        " ,Budget = '" & txtBudget.Text & "' " & _
        " ,Used = '" & txtUsed.Text & "' " & _
        " WHERE CustomerID = '" & myDataGrid.DataKeys.Item(e.Item.ItemIndex) & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()
        myDataGrid.EditItemIndex = -1
        myDataGrid.ShowFooter = True
        BindData()
    End Sub
End Class
 Ref : ASP.NET DataGrid Control - SQL Server 2000,2005 - System.Data.SqlClient
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-09-24 21:10:56 | By :
                            webmaster |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |