我是一个Delphi程序员,最近刚开始搞ASP.Net。
在使用DataGrid的时候需要通过表格上的编辑来更新数据,请问有没有类似于Delphi中的不用写update语句的方来来实现对当前行的更新。
比如说Table1.Edit;
      。。
      Table1.post;(Delphi中的方式)

解决方案 »

  1.   

    <form id="Form1" method="post" runat="server">
    <asp:datagrid id="dgOrder" runat="server" Height="515px" Width="718px" AutoGenerateColumns="False" AllowSorting="True" CellPadding="4" BorderWidth="1px" BorderColor="#A0ABEB" PageSize="15" BorderStyle="Solid" BackColor="White" GridLines="Vertical" ForeColor="Black" AllowPaging="True" ShowFooter="True">
    <SelectedItemStyle ForeColor="White" BackColor="Black"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
    <HeaderStyle HorizontalAlign="Center" ForeColor="White" BorderColor="#6876C5" BackColor="#6876C5"></HeaderStyle>
    <FooterStyle ForeColor="White" BackColor="#6876C5"></FooterStyle>
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <FONT face="">
    <asp:CheckBox id="Cb" runat="server"></asp:CheckBox></FONT>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="orderid" SortExpression="orderid" HeaderText="ID">
    <HeaderStyle Width="180px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="ShipCountry" SortExpression="ShipCountry" HeaderText="ShipCountry">
    <HeaderStyle Width="180px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="ShippedDate" SortExpression="ShippedDate" HeaderText="ShippedDate" DataFormatString="{0:d}">
    <HeaderStyle Width="180px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Freight" SortExpression="Freight" HeaderText="Freight">
    <HeaderStyle Width="180px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="ShipAddress" SortExpression="ShipAddress" HeaderText="ShipAddress">
    <HeaderStyle Width="480px"></HeaderStyle>
    </asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="Black" Position="TopAndBottom" BackColor="White" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
    </form>
      

  2.   

    '得到数据视图,参数为要排序的列
    Private Function GetDv(ByVal strSort As String) As DataView
            '定义数据库连接
            Dim dv As DataView
            Dim CN As New SqlConnection()
            Try
                '初始化连接字符串
                CN.ConnectionString = "data source=pmserver;
                initial catalog=Northwind;persist security info=False;user id=sa;Password=sa;"
                CN.Open()
    '从NorthWind得到orders表的数据
                Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from orders", CN)
                Dim ds As New DataSet()
                adp.Fill(ds)
                '得到数据视图
                dv = ds.Tables(0).DefaultView
            Catch ex As Exception
    #If DEBUG Then
                Session("Error") = ex.ToString()
                Response.Redirect("../error.aspx")        '跳转程序的公共错误处理页面
    #End If
            Finally
                '关闭连接
                CN.Close()
            End Try
            '排序
            dv.Sort = strSort
            Return dv
        End Function    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
        Handles MyBase.Load
            If Not IsPostBack Then
                ViewState("strSort") = "orderid"
                dgOrder.DataSource = GetDv(ViewState("strSort").ToString())
                dgOrder.DataBind()
            End If
        End Sub
    '排序
        Private Sub dgOrder_SortCommand(ByVal source As Object, 
        ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgOrder.SortCommand
            dgOrder.CurrentPageIndex = 0
       '得到排序的列
            ViewState("strSort") = e.SortExpression.ToString()
            dgOrder.DataSource = GetDv(ViewState("strSort").ToString())
            dgOrder.DataBind()
        End Sub '分页
        Private Sub dgOrder_PageIndexChanged(ByVal source As Object, 
        ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgOrder.PageIndexChanged
       '得到分页的页号
            dgOrder.CurrentPageIndex = e.NewPageIndex
            dgOrder.DataSource = GetDv(ViewState("strSort").ToString())
            dgOrder.DataBind()
        End Sub 
      

  3.   


    通过调用数据适配器的 Update 方法将更改从数据集发送到数据库: // C#
    sqlDataAdapter1.Update(DataSet);
    DataGrid1.DataBind();ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vbcon/html/vbwlkWalkthroughUsingDataGridWebControlToReadWriteData.htm
      

  4.   

    其实非常简单,就是用SqlDataAdapter的update方法就行了。主要代码例如下所示:
    private void update()    {      string link = ConfigurationSettings.AppSettings["link_local"].ToString();      SqlConnection conn = new SqlConnection(link);       SqlDataAdapter da = new SqlDataAdapter("SELECT order_id, contract FROM linhai", conn);       DataSet ds = new DataSet();      da.Fill(ds, "linhai");         da.UpdateCommand = new SqlCommand("UPDATE linhai SET contract = @contract " + "WHERE order_id = @order_id" , conn);      da.UpdateCommand.Parameters.Add("@contract", SqlDbType.NVarChar, 15, "contract");      da.UpdateCommand.Parameters.Add("@order_id", SqlDbType.NVarChar, 15, "order_id");            ds.Tables["linhai"].Rows[0]["contract"] = "PPP";      da.Update(ds.Tables[0]);    }
      

  5.   

    我Delphi从1用到7,很了解Delphi的DataAware方式和ASP.NET数据绑定间的区别,我可以清楚地告诉你,在ASP.NET中你必须自己写设局更新代码,没什么自动的——除非你自己开发一套类似的DataAware组件。出现这种问题是因为.NET Web数据绑定有一个假设的前提,就是假定Web页面上的多数数据都是“只读”的,不会更新。呵呵
      

  6.   

    ms也已经意识到这样做虽然可以给与程序员很大的操控数据更新的能力,但是相应的很多简单任务也要写重复的代码,造成生产力下降。所以在新版本的ASP.NET 2.0中,已经实现了类似Delphi Data Aware控件的能力,允许进行常规的自动更新动作,无须编写代码。