2013-05-14 139 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,它就可以工作。