更新gridview行 [英] Updating a gridview row

查看:39
本文介绍了更新gridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2013社区构建我的第一个asp.net网站,并使用gridview在SQL Server表中显示数据。  gridview由存储过程填充,我想将更新功能添加到gridview。 在Sql Server中,我
创建了一个存储过程来处理更新。 它接受3个输入,关键字段和数据字段的原始/新值。 很简单,存储过程如下所示:

I am building my first asp.net website using VS 2013 Community and using a gridview to display data in a SQL Server table.  The gridview is populated by a stored procedure and I want to add update functionality to the gridview.  In Sql Server I have created a stored procedure to process the update.  It accepts 3 inputs, the key field and the original/new values of a data field.  Quite simply the stored procedure looks like this:

更新<表>

Update <Table>

设置<数据列> =新值

Set <data column> = New Value

来自<表>

From <Table>

其中<主键> =<键字段>和<数据列> =旧值;

Where <Primary Key>=<key field> and <data column> = Old Value;

以确保其他用户是否已经更改了值,他们的更新不会被覆盖。

to ensure if another user has already changed the value their update will not be overwritten.

不幸的是我找不到任何如何以编程方式为存储过程设置3个参数的示例。 有人可以建议我怎么做到这一点吗? 

Unfortunately I can't find any examples of how to set the 3 parameters for the stored procedure programmatically.  Can someone advise how I can do this please? 

推荐答案

我似乎找到了一个解决方案,但是使用了存储过程,(我个人更喜欢使用),但我欢迎任何关于我的解决方案的适用性的评论。

I seem to have found a solution but dropped using the stored procedure, (which personally I'd prefer to use), but I'd welcome any comments on how suitable my solution is.

这是我的解决方案,为GridView控件编写RowUpdating事件:

Here is my solution by coding the RowUpdating event for the GridView control:

Private Sub GridView1_RowUpdating(sender As Object,e As GridViewUpdateEventArgs)处理GridView1.RowUpdating

        Dim res As Integer

        Dim Key As Integer

        Dim OldVal As Single

        Dim NewVal As Single

       使用GridView1为
            Key = Convert.ToInt32(e.Keys(0).ToString)

            OldQty = Convert.ToSingle(e.OldValues(" Qty")。ToString)

            NewQty = Convert.ToSingle(e.NewValues(" Qty")。ToString)

            SqlDataSource1.UpdateCommand =" Update< Table>设置< Column> =" &安培; NewQty& "Where< Key> ='" &安培;关键& "'和< Column> =" &安培;
OldQty

            res = SqlDataSource1.Update

           如果res = 0那么'更新失败,因为另一个用户已经更改了数据

              ;&NBSP;&NBSP; '让用户知道空文本框中的更新失败

           否则返回1
               'set textbox =""

           结束如果

       结束与$
   结束子

Private Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim res As Integer
        Dim Key As Integer
        Dim OldVal As Single
        Dim NewVal As Single
        With GridView1
            Key = Convert.ToInt32(e.Keys(0).ToString)
            OldQty = Convert.ToSingle(e.OldValues("Qty").ToString)
            NewQty = Convert.ToSingle(e.NewValues("Qty").ToString)
            SqlDataSource1.UpdateCommand = "Update <Table> Set <Column>=" & NewQty & "Where <Key>='" & Key & "' and <Column>=" & OldQty
            res = SqlDataSource1.Update
            If res = 0 Then 'the update failed as another user has already changed the data
                'let user know update failed in an empty textbox
            Else ' 1 was returned
                'set textbox = ""
            End If
        End With
    End Sub

我用过< Table>,< Key>和<列>而不是此发布的实际表和列名称。

I've used <Table>, <Key> and <Column> instead of actual table and column names for this posting.


这篇关于更新gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆