当前位置:编程学习 > C#/ASP.NET >>

在GridView取键值的问题


我现在想点“发货”,就执行一个SQL语句就OK了,也不用显示什么。。。
我在后台想取选择的这一行的“订单号”和“顾客号”去后台SQL表中对应,之后执行update XXX from 订单 where 订单号=‘选的这行的订单号’ and 顾客号='选的这行的订单号'。
其实说白了,我的表有两列共同为主键,这样怎么办?
“发货”的那个按钮应该用什么事件?我editing也用过,updating也用过,RowCommand也用过,就是取不到选到那行的列值…

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //this.GridView1.EditIndex = e.NewEditIndex;
            SqlConnection myConn = new SqlConnection(connStr);
            //订单号='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text + "'and 商品号='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text + "'";
            string mySql = "update 订单商品 set 状态='已发货' where 订单号='" + (GridView1.Rows[e.CommandSource].Cells[0].Controls[0]).Text + "'and 商品号='" + ((TextBox)GridView1.Rows[e.CommandSource].Cells[1].Controls[0]).Text + "'";
            string mySql1 = "update 订单 set 雇员编号='" + Session["UserName"] + "'where 订单号='" + ((TextBox)GridView1.Rows[e.CommandSource].Cells[0].Controls[0]).Text + "'and 商品号='" + ((TextBox)GridView1.Rows[e.CommandSource].Cells[1].Controls[0]).Text + "'";
            SqlCommand myCommand = new SqlCommand(mySql, myConn);
            SqlCommand myCommand1 = new SqlCommand(mySql1, myConn);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myCommand1.ExecuteNonQuery();
            myConn.Close();
        }
--------------------编程问答-------------------- 例子

        <asp:Button ID = "btnSelectItem " Text= "发货 " runat= "server " CommandName= "Update " CommandArgument= " <%# DataBinder.Evel(DataItem.Comtainer, "主键列名")%> " > 
            



            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
            { 
                if(e.CommandName == "Update ")
                {
                    int id =Convert.ToInt32(e.CommandArgument); //取出该行主键
                } 
            }
--------------------编程问答-------------------- 那“该行主键”是什么啊? --------------------编程问答--------------------
两个值通过属性或者隐藏域等其他方式绑定是正常的。
但是有两个都是主键我很怀疑你的程序设计了。 --------------------编程问答--------------------
引用 1 楼 lizeyuan8238386 的回复:
例子
XML/HTML code?12        <asp:Button ID = "btnSelectItem " Text= "发货 " runat= "server " CommandName= "Update " CommandArgument= " <%# DataBinder.Evel(DataItem.Comtainer, "主键列名")%> " > ……

+1 正解 --------------------编程问答-------------------- 嗯?一个表中不能两列合起来为一个主键吗? --------------------编程问答--------------------
引用 1 楼 lizeyuan8238386 的回复:
例子
XML/HTML code?12        <asp:Button ID = "btnSelectItem " Text= "发货 " runat= "server " CommandName= "Update " CommandArgument= " <%# DataBinder.Evel(DataItem.Comtainer, "主键列名")%> " > ……


然后我应该怎么写SQL语句啊? --------------------编程问答--------------------
引用 3 楼 fengyarongaa 的回复:
两个值通过属性或者隐藏域等其他方式绑定是正常的。
但是有两个都是主键我很怀疑你的程序设计了。

嗯?一个表中不能两列合起来为一个主键吗?  --------------------编程问答-------------------- 我觉得可以添加个编号 --------------------编程问答--------------------
引用 7 楼 vickyulin 的回复:
引用 3 楼 fengyarongaa 的回复:两个值通过属性或者隐藏域等其他方式绑定是正常的。
但是有两个都是主键我很怀疑你的程序设计了。
嗯?一个表中不能两列合起来为一个主键吗?


可以,但是如果是组合型的主键,就还是必须有一个唯一主键。个人见解。 --------------------编程问答--------------------

主键必须能够惟一地标识表中的某一条记录
-----------------------------------------------
引用 6 楼 vickyulin 的回复:
引用 1 楼 lizeyuan8238386 的回复:例子
XML/HTML code?12        <asp:Button ID = "btnSelectItem " Text= "发货 " runat= "server " CommandName= "Update " CommandArgument= " <%# DataBinder.Evel(DataIte……

你都取到了ID,其他的不是很好弄了 --------------------编程问答--------------------
引用 6 楼 vickyulin 的回复:
引用 1 楼 lizeyuan8238386 的回复:例子
XML/HTML code?12        <asp:Button ID = "btnSelectItem " Text= "发货 " runat= "server " CommandName= "Update " CommandArgument= " <%# DataBinder.Evel(DataIte……


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
            { 
                if(e.CommandName == "Update ")
                {
                    int id =Convert.ToInt32(e.CommandArgument); //取出该行主键
                    string sql = "update order set money=money+参数 where id="+id+" "
                } 
            }

--------------------编程问答--------------------                 if(e.CommandName == "Update ")
                {
                    int id =Convert.ToInt32(e.CommandArgument); //取出该行主键
                    string sql = "update order set money=money+参数 where id="+id+" "
                } 

都取到值了还不能实现么?想要的都有了那就要靠自己了
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,