DataGrid中CheckBox已经触发CheckedChanged事件       
    但是不知道改如何取得CheckBox所在行的DataKeyField值,请教一下大家       
        
    下面是我的代码:       
    protected       void       DataGrid1_ItemCreated(object       sender,       DataGridItemEventArgs       e)                                           
    {       
    CheckBox       c=(CheckBox)e.Item.FindControl("Up");       
    if       (c!=null)       
    {       
    c.CheckedChanged       +=new       EventHandler(c_CheckedChanged);       
    }       
        
    }       
        
        
        
    private       void       c_CheckedChanged(object       sender,       EventArgs       e)       
    {       
    在这里如何取得CheckBox所在行的DataKeyField值       
    }我只要触发事件哪行的值,不知道2003里面行不行,不要用For的,那样全遍历没什么意义,也不需要触发CheckedChanged事件,
那位高手指点下

解决方案 »

  1.   

    试下这个思路   
    protected       void       DataGrid1_ItemCreated(object       sender,       DataGridItemEventArgs       e)                                            
        {        
        CheckBox       c=(CheckBox)e.Item.FindControl("Up");        
        if       (c!=null)        
        {        
        c.CheckedChanged       +=new       EventHandler(c_CheckedChanged);    
        把该行的DataKeys存入Session    
        }        private       void       c_CheckedChanged(object       sender,       EventArgs       e)        
        {        
           通过Session获取你所存储的DataKey
             
        } 我现在在外地无法给你调试代码,这种方法应该可行
      

  2.   

    感觉直接取取不到,另外,你这种写法,checkbox选择的时候,c_CheckedChanged会址行吗?每次都是从新绑定
      

  3.   

    用Session取到的值永远都是最后一行的,因为那是在ItemCreated事件中取的,谢谢你的回答
      

  4.   

    感觉直接取取不到,另外,你这种写法,checkbox选择的时候,c_CheckedChanged会址行吗?每次都是从新绑定那应该怎么写
      

  5.   

    需要自定义事件,仿照下例
    using System;namespace 带事件数据的事件
    {
        /// <summary>
        /// 带事件数据的事件类,从EventArgs继承
        /// </summary>
        class OnUserRequestEventArgs:EventArgs
        {
            private string inputText;
            public string InputText
            {
                get
                {
                    return inputText;
                }
                set
                {
                    inputText = value;
                }
            }
        }    /// <summary>
        /// 事件发送类
        /// </summary>
        class Class1
        {
            public delegate void UserRequest(object sender,OnUserRequestEventArgs e);
            public event UserRequest OnUserRequest;        public void run()
            {
                while(true)
                {
                    Console.WriteLine("请输入内容:");
                    string a=Console.ReadLine();
                    //if(a=="a")
                    //{
                    OnUserRequestEventArgs e1 = new OnUserRequestEventArgs();
                    e1.InputText = a;
                    OnUserRequest(this,e1);
                    //}
                }
            }
        }    /// <summary>
        /// 事件接收类
        /// </summary>
        class Class2
        {
            [STAThread]
            static void Main(string[] args)
            {
                Class1 c1 = new Class1();
                c1.OnUserRequest += new Class1.UserRequest(c1_OnUserRequest);
                c1.run();
            }        private static void c1_OnUserRequest(object sender, OnUserRequestEventArgs e)
            {
                Console.WriteLine("\t你输入的是:"+e.InputText);
            }
        }
    }
      

  6.   

    GridView里这样实现
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                string row=((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex.ToString();
                Response.Write(this.GridView1.DataKeys[Convert.ToInt32(row)].Value.ToString());
            }
    DataGrid也类似
      

  7.   

            string arr = "";
            bool tt = true;
            int aa = GridView1.Rows.Count;
            for (int i = 0; i < aa; i++)
            {
                if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
                {
                    if (tt)
                    {
                        arr += GridView1.Rows[i].Cells[1].Text;
                        tt = false;
                    }
                    else
                    {
                        string dd = ",";
                        arr += dd + GridView1.Rows[i].Cells[1].Text;
                    }            }
            }
            if (arr == "")
            {
                PComm.Comm.Show(this.Page, "对不起,请选择要删除的行!");
                return;
            }
            else
            {
                string sql2 = "delete from foreignExchange where foreignID in(" + arr + ")";
                SqlCommand cmd = new SqlCommand(sql2, cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
                PComm.Comm.Show(this.Page, "数据删除成功!");
                bind();
            }
      

  8.   


    string row = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex.ToString();
    Response.Write(this.GridView2.DataKeys[Convert.ToInt32(row)].Value.ToString()); 牛,学习
      

  9.   

    string row = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex.ToString(); 
    Response.Write(this.GridView2.DataKeys[Convert.ToInt32(row)].Value.ToString());  DataGrid 里 RowIndex 应该改成什么?
      

  10.   

    需要自定义事件,仿照下例 
    using System; namespace 带事件数据的事件 

        ///  <summary > 
        /// 带事件数据的事件类,从EventArgs继承 
        ///  </summary > 
        class OnUserRequestEventArgs:EventArgs 
        { 
            private string inputText; 
            public string InputText 
            { 
                get 
                { 
                    return inputText; 
                } 
                set 
                { 
                    inputText = value; 
                } 
            } 
        }     ///  <summary > 
        /// 事件发送类 
        ///  </summary > 
        class Class1 
        { 
            public delegate void UserRequest(object sender,OnUserRequestEventArgs e); 
            public event UserRequest OnUserRequest;         public void run() 
            { 
                while(true) 
                { 
                    Console.WriteLine("请输入内容:"); 
                    string a=Console.ReadLine(); 
                    //if(a=="a") 
                    //{ 
                    OnUserRequestEventArgs e1 = new OnUserRequestEventArgs(); 
                    e1.InputText = a; 
                    OnUserRequest(this,e1); 
                    //} 
                } 
            } 
        } 
    这个好像时winfrom里面的,web里能用吗?
      

  11.   

            string arr = ""; 
            bool tt = true; 
            int aa = GridView1.Rows.Count; 
            for (int i = 0; i  < aa; i++) 
            { 
                if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true) 
                { 
                    if (tt) 
                    { 
                        arr += GridView1.Rows[i].Cells[1].Text; 
                        tt = false; 
                    } 
                    else 
                    { 
                        string dd = ","; 
                        arr += dd + GridView1.Rows[i].Cells[1].Text; 
                    }             } 
            } 
            if (arr == "") 
            { 
                PComm.Comm.Show(this.Page, "对不起,请选择要删除的行!"); 
                return; 
            } 
            else 
            { 
                string sql2 = "delete from foreignExchange where foreignID in(" + arr + ")"; 
                SqlCommand cmd = new SqlCommand(sql2, cn); 
                cn.Open(); 
                cmd.ExecuteNonQuery(); 
                cn.Close(); 
                PComm.Comm.Show(this.Page, "数据删除成功!"); 
                bind(); 
            }我要是选了两个以上,你知道我是要用那个,用for我不直接在提交的时候写就行了,还要选下刷新下?折磨服务器?
    不过还是谢谢你的回答
      

  12.   

    seedling_lq 还在吗?
    string row = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex.ToString();  
    Response.Write(this.GridView2.DataKeys[Convert.ToInt32(row)].Value.ToString());   DataGrid 里 RowIndex 应该改成什么?
      

  13.   

    RowIndex->ItemIndex?
    试试看。
      

  14.   

    ((DataGridItem)(((CheckBox)sender).Parent.Parent)).ItemIndex.ToString()晕,开始看花了 ,写成 DataGrid  不好意思 谢谢
      

  15.   

    你看你的
    ((DataGridItem)(((CheckBox)sender).Parent.Parent))结果是什么
    如果是DataGridItem 就肯定有ItemIndex,
    手上没有2003,没法测
      

  16.   

    你看你的 
    ((DataGridItem)(((CheckBox)sender).Parent.Parent))结果是什么 
    如果是DataGridItem 就肯定有ItemIndex, 
    手上没有2003,没法测呵呵,头晕眼花的,一时没注意,要深刻检讨  :)