none
gridview更新获取主键 RRS feed

  • 问题

  •       private void BindGrid()
        {
            GridView1.DataSource = this.DataSource;
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
        }
       protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
       
            string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取的id是GridView所在行的值,而不是主键的值
            string computer = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLOrderState")).SelectedValue;
            string mouse = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLHandled")).SelectedValue;
            string sqlStr = "update tree set computer='" + computer + "',mouse='" + mouse + "' where ID=" + ID + "";
            Common.ExecuteSql(sqlStr);
            GridView1.EditIndex = -1;
            BindGrid();
        }
    我想问一下上面获取的ID是gridview当前在表中的所在行数,可我想获得的是当前行的id主键的值,我想问一下有没有什么办法获取要更新所在行的主键的值。
    2009年10月11日 4:06

答案

  • 这个是用SQL Server2000自带的数据库做的测试:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
        Response.Write(this.GridView1.DataKeys[e.RowIndex].Value);
      }
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
        AutoGenerateEditButton="true" DataSourceID="SqlDataSource1" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
          <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
          <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
          <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
          <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
          <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
          <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
          <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
          <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
          <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
          <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
          <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" 
        SelectCommand="SELECT * FROM [Customers]" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [CustomerID] = @CustomerID"
        DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)">
        <DeleteParameters>
          <asp:Parameter Name="CustomerID" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
          <asp:Parameter Name="CompanyName" Type="String" />
          <asp:Parameter Name="ContactName" Type="String" />
          <asp:Parameter Name="ContactTitle" Type="String" />
          <asp:Parameter Name="Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="Region" Type="String" />
          <asp:Parameter Name="PostalCode" Type="String" />
          <asp:Parameter Name="Country" Type="String" />
          <asp:Parameter Name="Phone" Type="String" />
          <asp:Parameter Name="Fax" Type="String" />
          <asp:Parameter Name="CustomerID" Type="String" />
        </UpdateParameters>
        <InsertParameters>
          <asp:Parameter Name="CustomerID" Type="String" />
          <asp:Parameter Name="CompanyName" Type="String" />
          <asp:Parameter Name="ContactName" Type="String" />
          <asp:Parameter Name="ContactTitle" Type="String" />
          <asp:Parameter Name="Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="Region" Type="String" />
          <asp:Parameter Name="PostalCode" Type="String" />
          <asp:Parameter Name="Country" Type="String" />
          <asp:Parameter Name="Phone" Type="String" />
          <asp:Parameter Name="Fax" Type="String" />
        </InsertParameters>
      </asp:SqlDataSource>
      </form>
    </body>
    </html>
    

    【孟子E章】
    2009年10月11日 4:56
    版主

全部回复

  • this.GridView1.DataKeys[e.RowIndex].Value
    就是主键
    【孟子E章】
    2009年10月11日 4:54
    版主
  • 这个是用SQL Server2000自带的数据库做的测试:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
        Response.Write(this.GridView1.DataKeys[e.RowIndex].Value);
      }
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
        AutoGenerateEditButton="true" DataSourceID="SqlDataSource1" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
          <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
          <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
          <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
          <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
          <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
          <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
          <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
          <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
          <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
          <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
          <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" 
        SelectCommand="SELECT * FROM [Customers]" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [CustomerID] = @CustomerID"
        DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)">
        <DeleteParameters>
          <asp:Parameter Name="CustomerID" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
          <asp:Parameter Name="CompanyName" Type="String" />
          <asp:Parameter Name="ContactName" Type="String" />
          <asp:Parameter Name="ContactTitle" Type="String" />
          <asp:Parameter Name="Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="Region" Type="String" />
          <asp:Parameter Name="PostalCode" Type="String" />
          <asp:Parameter Name="Country" Type="String" />
          <asp:Parameter Name="Phone" Type="String" />
          <asp:Parameter Name="Fax" Type="String" />
          <asp:Parameter Name="CustomerID" Type="String" />
        </UpdateParameters>
        <InsertParameters>
          <asp:Parameter Name="CustomerID" Type="String" />
          <asp:Parameter Name="CompanyName" Type="String" />
          <asp:Parameter Name="ContactName" Type="String" />
          <asp:Parameter Name="ContactTitle" Type="String" />
          <asp:Parameter Name="Address" Type="String" />
          <asp:Parameter Name="City" Type="String" />
          <asp:Parameter Name="Region" Type="String" />
          <asp:Parameter Name="PostalCode" Type="String" />
          <asp:Parameter Name="Country" Type="String" />
          <asp:Parameter Name="Phone" Type="String" />
          <asp:Parameter Name="Fax" Type="String" />
        </InsertParameters>
      </asp:SqlDataSource>
      </form>
    </body>
    </html>
    

    【孟子E章】
    2009年10月11日 4:56
    版主
  • 楼上同胞, DataKeyNames="CustomerID" 不就是主键吗???
    疑问。
    2009年10月12日 3:01
  • 同意!用过就知道了。
    主键在后台中就是这样获取的。

    2009年10月12日 3:25