0

처음으로 나는 sqlserver 2008 데이터베이스 테이블의 데이터로 datagridview를 채웠다. 이제 데이터가 포함 된 datagridview에 다중 행이 생겼다. 다른 행을 업데이트하려고 시도했지만 다른 것을 대체한다. 를 업데이트하려고 스피 행 데이터에 의한 행의 데이터가 업데이트 문에 대한 코드는 Plz은 당신이 열심히 행을 구분 한 나에게vb.net을 사용하여 gridview의 데이터 업데이트

Dim cmd As New SqlCommand("Update EmployeeDetail Set Salary = '" &  
dgvEmpDetail.Rows(0).Cells(1).Value & "', Experience ='" & 
dgvEmpDetail.Rows(0).Cells(2).Value & "', Skills='" & 
dgvEmpDetail.Rows(0).Cells(3).Value 
& "' where Emp_ID = '" & dgvEmpDetail.Rows(0).Cells(0).Value & "'", con) 
con.Open()               
cmd.ExecuteNonQuery()  
con.Close() 
+0

이것은'C#'에 관한 것이 아닙니다. 그것은'vb.net'입니다. 관련성없는 태그를 추가하지 마십시오. –

+0

에이 코드를 추가하면 첫 번째 행만 업데이트됩니다. 그리고 당신의 질문은 아마도 내가 충분히 명확하지 않습니다. – HengChin

+0

여러 행을 업데이트해야하지만 행의 셀 값을 업데이트하려고하면 다른 모든 레코드가 업데이트하려는 행 값으로 대체됩니다. – Muazzam

답변

0

도움

아래에 주어진다 - dgvEmpDetail.Rows (0).

나는 이것을 루프에서 호출한다고 상상한다.

For i As Integer = 0 To dgvEmpDetail.Rows.Count - 1 
     Dim cmd As New SqlCommand("Update EmployeeDetail Set Salary = '" & dgvEmpDetail.Rows(i).Cells(1).Value & "', Experience ='" & dgvEmpDetail.Rows(i).Cells(2).Value & "', Skills='" & dgvEmpDetail.Rows(i).Cells(3).Value()& "' where Emp_ID = '" & dgvEmpDetail.Rows(i).Cells(0).Value & "'", con) 
     con.Open() 
     cmd.ExecuteNonQuery() 
     con.Close() 
    Next 

코드는 SQL 삽입의 영향을 받기 쉽습니다. 업데이트 된 SQL을 저장 프로 시저에 저장해야합니다 - 빠르고 안전합니다!

+0

답장을 보내 주셔서 감사합니다. – Muazzam

0
Protected Sub Page_Load() 

    If Not Page.IsPostBack Then 
     ' Create a new table. 
     Dim taskTable As New DataTable("TaskList") 

     ' Create the columns. 
     taskTable.Columns.Add("Id", GetType(Integer)) 
     taskTable.Columns.Add("Description", GetType(String)) 
     taskTable.Columns.Add("IsComplete", GetType(Boolean)) 

     'Add data to the new table. 
     For i = 0 To 19 
     Dim tableRow = taskTable.NewRow() 
     tableRow("Id") = i 
     tableRow("Description") = "Task " + i.ToString() 
     tableRow("IsComplete") = False 
     taskTable.Rows.Add(tableRow) 
     Next 

     'Persist the table in the Session object. 
     Session("TaskTable") = taskTable 

     'Bind data to the GridView control. 
     BindData() 
    End If 

    End Sub 

    Protected Sub TaskGridView_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) 
    TaskGridView.PageIndex = e.NewPageIndex 
    'Bind data to the GridView control. 
    BindData() 
    End Sub 

    Protected Sub TaskGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) 
    'Set the edit index. 
    TaskGridView.EditIndex = e.NewEditIndex 
    'Bind data to the GridView control. 
    BindData() 
    End Sub 

    Protected Sub TaskGridView_RowCancelingEdit() 
    'Reset the edit index. 
    TaskGridView.EditIndex = -1 
    'Bind data to the GridView control. 
    BindData() 
    End Sub 

    Protected Sub TaskGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) 
    'Retrieve the table from the session object. 
    Dim dt = CType(Session("TaskTable"), DataTable) 

    'Update the values. 
    Dim row = TaskGridView.Rows(e.RowIndex) 
    dt.Rows(row.DataItemIndex)("Id") = (CType((row.Cells(1).Controls(0)), TextBox)).Text 
    dt.Rows(row.DataItemIndex)("Description") = (CType((row.Cells(2).Controls(0)), TextBox)).Text 
    dt.Rows(row.DataItemIndex)("IsComplete") = (CType((row.Cells(3).Controls(0)), CheckBox)).Checked 

    'Reset the edit index. 
    TaskGridView.EditIndex = -1 

    'Bind data to the GridView control. 
    BindData() 
    End Sub 

    Private Sub BindData() 
    TaskGridView.DataSource = Session("TaskTable") 
    TaskGridView.DataBind() 
    End Sub 

</script> 
0

데이터베이스에 대한 데이터 공급기로 텍스트 상자에 대한 액세스 연결이 있습니다. 원하는 경우 SQL로 변경하십시오. 코드는 다음과 같습니다 :

Imports System 
Imports System.Data 
Imports System.Data.OleDb 
Public Class Form2 
    Dim conaccess As New OleDbConnection 
    Dim conreader As OleDbDataReader 
    Dim concmd As New OleDbCommand 


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

     DataGridView1.EditMode = False 
     conaccess.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=d:\vijay.mdb" 
     conaccess.Open() 
     loadGrid() 
    End Sub 

    Private Sub loadGrid() 
     Dim access As String 
     access = "select * from vijay" 
     Dim DataTab As New DataTable 
     Dim DataAdap As New OleDbDataAdapter(access, conaccess) 
     DataAdap.Fill(DataTab) 
     DataGridView1.DataSource = DataTab 
    End Sub 

    Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click 

     Dim no As String 
     no = "select Max(ID) from vijay" 
     Dim concmd As New OleDbCommand(no, conaccess) 
     conreader = concmd.ExecuteReader 
     If (conreader.Read) Then 
      If (IsDBNull(conreader(0))) Then 
       id_txt.Text = "1" 
      Else 
       id_txt.Text = conreader(0) + 1 
      End If 
      name_txt.Clear() 
      branch_txt.Clear() 
      age_txt.Clear() 
      class_txt.Clear() 
      gen_txt.Clear() 
     End If 
    End Sub 

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
     Dim i As Integer 

     i = DataGridView1.CurrentRow.Index 
     Try 
      id_txt.Text = DataGridView1.Item(0, i).Value 
      name_txt.Text = DataGridView1.Item(1, i).Value 
      class_txt.Text = DataGridView1.Item(2, i).Value 
      gen_txt.Text = DataGridView1.Item(3, i).Value 
      branch_txt.Text = DataGridView1.Item(4, i).Value 
      age_txt.Text = DataGridView1.Item(5, i).Value 
     Catch ex As Exception 

     End Try 

    End Sub 


    Private Sub del_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles del_btn.Click 
     Dim delcmd As New OleDbCommand("delete from vijay where id=" & id_txt.Text & " ", conaccess) 
     delcmd.ExecuteNonQuery() 
     MsgBox("Record is deleted") 
     loadGrid() 
     id_txt.Clear() 
     name_txt.Clear() 
     branch_txt.Clear() 
     age_txt.Clear() 
     class_txt.Clear() 
     gen_txt.Clear() 
    End Sub 

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click 
     Dim access As String = String.Format("INSERT INTO vijay (Name,Class,Branch,Gender,Age) VALUES('{0}','{1}','{2}','{3}','{4}')", name_txt.Text, class_txt.Text, branch_txt.Text, gen_txt.Text, age_txt.Text) 
     concmd.Connection = conaccess 
     concmd.CommandText = access 
     concmd.ExecuteNonQuery() 
     MsgBox("Record Successfully Saved") 
     loadGrid() 
     id_txt.Clear() 
     name_txt.Clear() 
     branch_txt.Clear() 
     age_txt.Clear() 
     class_txt.Clear() 
     gen_txt.Clear() 
    End Sub 

    Private Sub up_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up_btn.Click 
     Dim access As String 
     access = "UPDATE vijay SET Name = '" & name_txt.Text & "', Age = '" & age_txt.Text & "', Gender ='" & gen_txt.Text & "' , Branch ='" & branch_txt.Text & "' , Class = '" & class_txt.Text & "' where id=" & id_txt.Text & "" 
     Dim cmd As New OleDbCommand(access, conaccess) 
     cmd.ExecuteNonQuery() 
     loadGrid() 
     id_txt.Clear() 
     name_txt.Clear() 
     branch_txt.Clear() 
     age_txt.Clear() 
     class_txt.Clear() 
     gen_txt.Clear() 

    End Sub 
End Class