我现在要求这样:当编辑修改时,DropDownList显示CU_NAME_CODE字段值(CU_ID.DataTextField = "CU_NAME_CODE")。修改完后返回给CS文件时,它的值为CU_ID字段值(CU_ID.DataValueField = "CU_ID")。1、先看我的表结构。SQL> desc sales;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 S_ID                                               NUMBER(4)
 S_NO                                               NUMBER(5)
 CU_ID                                              NUMBER(4)
SQL> desc customer;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 CU_ID                                     NOT NULL NUMBER(4)
 CU_NO                                              NUMBER(2)
 CU_NAME                                            VARCHAR2(50)
 CU_NAME_CODE                                       VARCHAR2(20)
SQL> ALTER TABLE sales  ADD CONSTRAINT FK_sales_cu_id FOREIGN KEY (cu_id) REFERENCES customer(cu_id);表已更改。SQL> CREATE OR REPLACE VIEW V_SALE AS
  2  SELECT
  3  a.S_ID, a.S_NO, a.CU_ID,b.CU_NAME_CODE
  4  FROM WK.SALES a,customer b
  5  where a.CU_ID=b.CU_ID;视图已建立。2、再看我的前台ASPX代码。<asp:datagrid id="SALES"                          .........>
.......
<EditItemTemplate>
<asp:DropDownList id="CU_ID" runat="server" AutoPostBack="True" SelectedIndex='<%# DataBinder.Eval(Container.DataItem, "CU_ID")) %>'></asp:DropDownList></EditItemTemplate>
......
</asp:datagrid>
最后看我的CS代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{

BindGridToSource();
BindGridToCustomer();

}
}
public void BindGridToSource()
{


OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
string mySelectQuery = "SELECT * FROM V_SALE order by S_ID";
OracleDataAdapter myCommand = new OracleDataAdapter(mySelectQuery,conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "sales");
this.SALES.DataKeyField="S_ID";
DataTable  myDt= ds.Tables["sales"];
DataView dv = ds.Tables["sales"].DefaultView;;
Session["MY_SALES"]=myDt;
SALES.DataSource = myDt;
this.SALES.DataBind();
}


public void BindGridToCustomer()
{
OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
string mySelectQuery = "SELECT * FROM CUSTOMER";
OracleCommand myCommand = new OracleCommand(mySelectQuery,conn);
conn.Open();
OracleDataReader dr = myCommand.ExecuteReader();
CU_ID.DataSource = dr;
CU_ID.DataTextField = "CU_NAME_CODE";
CU_ID.DataValueField = "CU_ID";
CU_ID.DataBind();
dr.Close();
conn.Close();
}

解决方案 »

  1.   

    需求更正一下:我现在要求这样:当编辑修改时,DropDownList显示所有CUSTOMER表中的CU_NAME_CODE字段值,并且默认显示与SALES表CU_ID相对应的CU_NAME_CODE字段值(CU_ID.DataTextField = "CU_NAME_CODE")。修改完后返回给CS文件时,它的值为CU_ID字段值(CU_ID.DataValueField = "CU_ID")。
      

  2.   

    http://singlepine.cnblogs.com/articles/266538.html
      

  3.   

    谢谢小山,编辑、修改已经都没问题了。
    但我现在发现一个问题,当我选择“添加一行”addrow按钮时,提示下面错误。“/WebApplicationCSharp”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 268:
    行 269:
    行 270: DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
    行 271: }
    行 272:
     源文件: c:\wk\webapplicationcsharp\sales.aspx.cs    行: 270 看看我的CS代码。 public void addrow(object s,EventArgs e)
    {    
    DataTable dt;
    DataRow dr;
    dt=(DataTable)Session["MY_SALES"]; 
    dr=dt.NewRow();
    dt.Rows.Add(dr);
    //更新内存的Dataset
    Session["MY_SALES"]=dt;
    Int32 Index;
    Index=dt.DefaultView.Count;
    this.SALES.DataSource=dt;
    this.SALES.DataKeyField = "S_ID";
    this.SALES.DataBind();
    this.SALES.CurrentPageIndex=SALES.PageCount - 1;
    this.SALES.DataBind(); //为添加的新行打开编辑模式
    LBTN_Add.Visible = false;
    this.SALES.EditItemIndex = SALES.Items.Count - 1;
    this.SALES.DataBind();
    } public void BindGridToSource()
    {


    OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
    string mySelectQuery = "SELECT * FROM V_SALES order by S_ID";
    OracleDataAdapter myCommand = new OracleDataAdapter(mySelectQuery,conn);
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "sales");
    this.SALES.DataKeyField="S_ID";
    DataTable  myDt= ds.Tables["sales"];
    DataView dv = ds.Tables["sales"].DefaultView;;
    Session["MY_SALES"]=myDt;
    SALES.DataSource = myDt;
    this.SALES.DataBind();
    }


    public void SALES_ItemDataBound(object sender, DataGridItemEventArgs e)
    { OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
    string mySelectQuery = "SELECT * FROM CUSTOMER";
    OracleDataAdapter myCommand = new OracleDataAdapter(mySelectQuery,conn);
    DataSet dr = new DataSet();
    myCommand.Fill(dr, "customer");
    if(e.Item.ItemType==ListItemType.EditItem)
    {
               DropDownList DDLCU_ID=(DropDownList)e.Item.FindControl("CU_ID");
    DDLCU_ID.DataSource = dr.Tables["customer"].DefaultView;
    DDLCU_ID.DataTextField = "CU_NAME_CODE";
    DDLCU_ID.DataValueField = "CU_ID";
    DDLCU_ID.DataBind();
    DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
    }



    }