2012-01-31 108 views
0

InsertUpdate之後刷新DataGridView控件時出現問題。源代碼:如何刷新dataGridView

獲取數據表,從表中的所有行並設置爲數據源:

Dim dt1 as DataTable = GetData("SELECT * FROM CLAIMSTATE ") 
dataGrid.DataSource = dt1 

更新事件如果ID被重視,插入如果不是:

Private Sub dataGrid_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGrid.RowLeave 

Dim row As DataGridViewRow = CType(sender, DataGridView).Rows(e.RowIndex) 
    Dim query As New StringBuilder("") 
    If row.Cells(0).Value & "" = "" Then 
    query.Append("INSERT INTO CLAIMSTATE ") 
    query.Append("(CST_CODE, CST_LABEL, CST_POINTS)") 
    query.Append("VALUES ") 
    query.Append("(?, ?, ?)") 
    Else 
    query.Append("Update CLAIMSTATE ") 
    query.Append("SET CST_CODE = ?, ") 
    query.Append("CST_LABEL = ?, ") 
    query.Append("CST_POINTS = ? ") 
    query.Append("WHERE CST_ID = ? ") 
    End If 
    Dim command As New OdbcCommand(query.ToString(), con) 
    command.Parameters.Add("@cst_code", OdbcType.Char).Value = row.Cells(1).Value 
    command.Parameters.Add("@cst_label", OdbcType.NVarChar).Value = row.Cells(2).Value 
    command.Parameters.Add("@cst_points", OdbcType.Decimal).Value = row.Cells(3).Value 
    command.Parameters.Add("@cst_id", OdbcType.BigInt).Value = row.Cells(0).Value 

    Dim res As Integer = ExecuteNonQuery(command) 
End Sub 

Public Function GetData(ByRef sqlQuery As String) As DataTable 
    Dim command As New OdbcCommand(sqlQuery, con) 
    Try 
     If con.State = ConnectionState.Closed Then 
     con.ConnectionString = conString 
     con.Open() 
     End If 
     Using dr As OdbcDataReader = command.ExecuteReader() 
     Dim dt As New DataTable() 
     dt.Load(dr) 
     Return dt 
     End Using 
     'con.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
     con.Close() 
     Return Null 
    End Try 
    End Function 

Public Function ExecuteNonQuery(ByRef command As OdbcCommand) As Integer 
Dim result As Integer = 0 
If con.State = ConnectionState.Closed Then 
    con.ConnectionString = conString 
    con.Open() 
End If 
'Dim command As New OdbcCommand(sqlQuery, conn) 
Try 
    'command.Connection = con 
    'Dim cmd As New OdbcCommand(sqlQuery, conn) 
    result = command.ExecuteNonQuery() 
Catch 
    result = 0 
    If con IsNot Nothing Then 
    con.Close() 
    command.Dispose() 
    End If 
Finally 
    command.Dispose() 
End Try 
Return result 
End Function 

我試圖從表中獲取所有記錄,並在方法結尾再次設置數據源,但它不起作用。

如果我把代碼:

dataGrid.Rows.Clear() 
dataGrid.Columns.Clear() 
dt1 = GetData("SELECT * FROM CLAIMSTATE ") 
dataGrid.DataSource = dt1 
事件方法的端

RowLeave我recive此錯誤:

"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"

上dataGrid.Rows.Clear(),但如果刪除線代碼Rows.Clear()和Columns.Clear(),執行dataGrid.DataSource = dt1之後的調試遊標返回到事件方法的開始,再次執行一些代碼,並在我收到一些錯誤「... reentrant調用SetCurrentCellAddressCore功能」!

請幫幫我!

回答

0

爲了解決這個問題我使用OdbcDataAdapter。我使用adapter.Update(dataTable)保存所有更改,然後再次填充數據表:adapter.fill(dataTable)。

0

這是一個C#類,我用它來連接搜索我的GridView。你所需要的應該與我想象的類似。

protected void lbSearch_Click(object sender, EventArgs e) 
    { 
     if (txtSearch.Text.Trim().Length > 0) 
     { 
      odsInbox.FilterExpression = 
       string.Format("(l_name LIKE '*{0}*') OR (f_name LIKE '*{0}*') OR (title LIKE '*{0}*')", 
       txtSearch.Text); 
     } 
     else 
     { 
      odsInbox.FilterExpression = string.Empty; 
     } 

     gvInbox.DataBind(); 
    } 

    protected void lbClear_Click(object sender, EventArgs e) 
    { 
     odsInbox.FilterExpression = string.Empty; 
     txtSearch.Text = ""; 
     gvInbox.DataBind(); 
    } 

我希望這能讓您走上正軌。

+0

DataGridView上的DataSource沒有方法Clear()和Add()。任何人有其他的想法,我怎麼可以刷新到datagridview? – wertyk 2012-02-01 06:19:30

+1

我發現這個網頁:http://stackoverflow.com/questions/2760657/failure-to-validate-but-cannot-remove-in-datagridview 我怎麼可以在vb.net寫這個: BeginInvoke(new Action(委託{dataGrid.DataSource = dt1;})); – wertyk 2012-02-01 12:34:24

+0

@wertyk對不起。 GridView ...不ReportViewer。我會爲此更新問題。 – 2012-02-01 14:21:31