2013-05-14 79 views
2

我有通過VS 2010(本地數據庫文件選項)創建的SQL Server Compact數據庫。 在表單加載(CategoryForm)我將數據庫中的值加載到DataGridView1。我還以編程方式添加了一個額外的ButtonColumn,我用於Delete部分。 問題是這樣的:從數據庫中刪除錯誤

  • 在初始表單加載時,我第一次按任何行刪除,它的工作原理。如果我再次按下它不起作用。
  • 第二次單擊按鈕時,我在Msgbox上得到的打印文本是按鈕文本! (刪除)(包括屏幕截圖)p.s如下所述,當我註釋掉額外的東西時,我會得到正確的返回值。

我試了一下:

  • 註釋掉一切SQL有關,只留下一部分在那裏我得到了RowIndex,並在該指數在特定單元格的值。我在MsgBox上打印它們。我得到的價值是正確的。例如,0索引和值test第一行文本test

下面是我的進步以及截圖:

CellContentClick方法:

Private Sub DataGridView1_CellContectClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick 

    Dim i As String 

    'If e.ColumnIndex = 1 Then 
    'If e.RowIndex >= 0 And e.RowIndex <= DataGridView1.Rows.Count - 1 Then 

    i = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString 
    MsgBox(e.RowIndex) 
    MsgBox(i) 
    SQLStringDelete = "DELETE FROM Category WHERE categoryname = '" & i & "'" 
    SQLConn.ConnectionString = ConnString 'Set the Connection String 
    SQLConn.Open() 'Open the connection 
    SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command 
    SQLCmd.CommandText = SQLStringDelete 'Sets the SQL String 
    SQLCmd.ExecuteNonQuery() 'Executes SQL Command 

    'Create Adapter 
    Dim DataAdapter As SqlCeDataAdapter = New SqlCeDataAdapter("SELECT categoryname FROM Category", SQLConn) 
    'Create DataSet 
    Dim Dataset As New DataSet 
    'fill the datset 
    DataAdapter.Fill(Dataset) 
    'attach dataset to the datagrid 
    With DataGridView1 
     .DataSource = Dataset.Tables(0) 
     .Columns(0).HeaderText = "" 
     .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
    End With 

    DataAdapter = Nothing 
    Dataset = Nothing 

    SQLConn.Close() 'Close the connection 



End Sub 

方法:

Private Sub CategoryForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     SQLConn.ConnectionString = ConnString 'Set the Connection String 
     SQLConn.Open() 'Open the connection 

     'Create Adapter 
     Dim DataAdapter As SqlCeDataAdapter = New SqlCeDataAdapter("SELECT categoryname FROM Category", SQLConn) 
     'Create DataSet 
     Dim Dataset As New DataSet 
     'fill the datset 
     DataAdapter.Fill(Dataset) 
     'attach dataset to the datagrid 
     With DataGridView1 
      .DataSource = Dataset.Tables(0) 
      .Columns(0).HeaderText = "" 
      .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
     End With 

     DataAdapter = Nothing 
     Dataset = Nothing 
     SQLConn.Close() 

     With buttonColumn 
      .Name = "DeleteButtonColumn" 
      .HeaderText = "" 
      .Text = "Delete" 
      .UseColumnTextForButtonValue = True 
      .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
      .Width = 50 
     End With 

     If DataGridView1.Columns.Count = 1 Then 
      DataGridView1.Columns.Add(buttonColumn) 
     End If 

    End Sub 

截圖:

Button text returned instead of cell value

Still there after several click :)

回答

0

原來,這裏autoregenerating在每個第二次點擊導致列重新安排的列。只需在表單加載和事件中設置DataGridView1.AutoGenerateColumns = false,它就可以工作。