GridView编辑代码示例(oracle+vs2008)

GridView更新,两种方法:

写进代码里面了.

一种是使用oraclecommand 的update语句

另一种是使用了oraclecommandbuilder方法

数据库表用的是sqlserver2000示例数据库NorthWind,转换成的oracle版的.用的里面的products表 

前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="_2008test.WebForm5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcancelingedit
="GridView1_RowCancelingEdit" 
            onrowediting
="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
            onselectedindexchanging
="GridView1_SelectedIndexChanging">
            
<Columns>
                
<asp:BoundField DataField="productid" ReadOnly="true"/>
                
<asp:BoundField DataField="productname" HeaderText="产品名" ReadOnly="true" Visible="true" />
                
<asp:BoundField DataField="unitprice" HeaderText="价格" Visible="true" />
                
<asp:CommandField ShowEditButton="True" />               
            
</Columns>
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

 

 

后台代码:

 

 

后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
namespace _2008test
{
    
public partial class WebForm5 : System.Web.UI.Page
    {
        OracleConnection conn;        
        
string constr = ConfigurationManager.ConnectionStrings["testdb3"].ConnectionString;
        
string strSQL = "select productid,productname,unitprice from  testproducts where categoryid=1";
        
string producttablename = "productTable";
        
void loadgrid()
        {
            
DataSet ds = new DataSet();
            OracleDataAdapter da
=new OracleDataAdapter(strSQL,constr);
            da.Fill(ds, producttablename);
            GridView1.DataSource 
= ds.Tables[producttablename];
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
               loadgrid(); 
            }
        }
        
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex 
= e.NewEditIndex;
            loadgrid();
        }
        
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex 
= -1;
            loadgrid();
        }
              
        
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
////方法一:使用command更新
            //OracleConnection conn = new OracleConnection(constr);
            
//string sqlstr = "update testproducts set unitprice='"
            
//    + Convert.ToDouble(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()) + "' "
            
//    + "where productid='" + Convert.ToDecimal(GridView1.Rows[e.RowIndex].Cells[0].Text) + "'";
           
 //OracleCommand com = new OracleCommand(sqlstr, conn);
            
//conn.Open();
            
//com.ExecuteNonQuery();
            
//conn.Close();
            
//GridView1.EditIndex = -1;
            
//loadgrid();

            
//方法二:使用oraclecommandbuilder更新;
            decimal productid = Convert.ToDecimal(GridView1.Rows[e.RowIndex].Cells[0].Text);
            TextBox pricetextbox 
= GridView1.Rows[e.RowIndex].Cells[2].Controls[0as TextBox;
            
double unitprice = Convert.ToDouble(pricetextbox.Text);            
            GridView1.EditIndex 
= -1;
            
//开始更新
            OracleDataAdapter da = new OracleDataAdapter(strSQL, constr);
            DataSet ds 
= new DataSet();
            da.Fill(ds, producttablename);
            
//建临时表
            DataTable dt = ds.Tables[producttablename];
            dt.PrimaryKey 
= new DataColumn[] 
            { 
                dt.Columns[
"productid"]
            };
            DataRow dr 
= dt.Rows.Find(productid);
            dr.SetField(
"unitprice", unitprice);
            OracleCommandBuilder cb 
= new OracleCommandBuilder(da);
            da.Update(ds, producttablename); 
            loadgrid();
        }
    }
}

 

 

posted @ 2009-12-17 17:55  PROS  阅读(915)  评论(0)    收藏  举报