100分求高人解答关于DataGrid选中行并取值的问题?问题具体如下:DataGrid绑出一个列表【列表一】,其中操作的四个选项是用复选框(CheckBoxList)勾选的,当选中某行的某个选项时(任意的),需要将该行的ID和选中选项的值取出,并将结果整理成列表【列表二】。求一个实现的方法,有代码的最好。多谢多谢!【列表一】
-------------------------------------------------
ID     标题                     操作
.
.
2      aaaa           口是  口否  口未知 口失效
3      bbbb           口是  口否  口未知 口失效
5      cccc           口是  口否  口未知 口失效
.
.
-------------------------------------------------
当在操作中,
选中ID=2行的“是”、“未知”和“失效”选项,
以及选中ID=3行的“是”、“未知”,
还有ID=5行的“否”、“失效”
其操作的显示结果为如下列表:
【列表二】
-------------------------------
ID     操作结果
2  是,未知,失效,
3  是,未知,
5  否,失效,
-------------------------------

解决方案 »

  1.   


        
       /// <summary>
       /// 在点击事件里面去调用  try 一 try 
       /// </summary>
        private void GetRight()
        {
            foreach (GridViewRow gvr in this.GridView1.Rows)//循环每一行
             {
                CheckBoxList chk = gvr.FindControl("CheckBoxList") as CheckBoxList;
                //找到该 CheckBoxList
                string itmesValue=string.Empty;//操作结果的值
                #region 循环这个CheckBoxList的项
                for (int i = 0; i < chk.Items.Count; i++)
                {
                    if (chk.Items[i].Selected)
                    {
                        itmesValue += chk.Items[i].Text + ",";//累加操作结果的文本
                    }
                }
                #endregion
               //如果为空(都没打钩)就不跟新此行或者更新空值
                if (string.IsNullOrEmpty( itmesValue))
                  {                /*
                     * 根据ID(DataKeys)更新到数据库-itmesValue-
                     */
                }
            }
        }
            /*
             * 接下去就显示数据..这里就不写了
             */
      

  2.   

    来晚了,蜘蛛正解。
    很简单的问题,重点是Selected取得选中行,加以处理。
      

  3.   


    GOOD GOOD STYDY , DAY DAY UP
      

  4.   

    这个事件时放在CheckBoxList的SelectedIndexChanged事件中,
    选中和取消选中的时候都有值,
    有没有更好的解决方案?
      

  5.   

    只要读取选中的那行record中的内容就可以了。
      

  6.   

    设置Datagrid的DataKeyField属性 
      

  7.   

    你绑定DataGrid的时候肯定没有绑定数据库中主键。给你个例子看看:   
      cn=new   OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data   Source="+Server.MapPath(ConfigurationSettings.AppSettings["ds"]));   
      string   strSQL="select   *   from   material,sort_material,storage   where   material.sort_num=sort_material.sort_num   and   storage.storage_num=material.storage_num   order   by   m_num";   
      da=new   OleDbDataAdapter(strSQL,cn);   
      ds=new   DataSet();   
      cn.Open();   
      da.Fill(ds);   
      DataGrid1.DataSource=ds;   
      DataGrid1.DataKeyField="m_num";//最好是数据库中的主键字段   
      DataGrid1.DataBind();   
      cn.Close();   
        
      //更新数据库   
      private   void   DataGrid1_UpdateCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)   
      {   
        
      TextBox   mytb1=(TextBox)e.Item.Cells[1].Controls[0];   
      TextBox   mytb2=(TextBox)e.Item.Cells[2].Controls[0];   
      TextBox   mytb3=(TextBox)e.Item.Cells[3].Controls[0];   
      TextBox   mytb4=(TextBox)e.Item.Cells[5].Controls[0];   
      cn=new   OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data   Source="+Server.MapPath(ConfigurationSettings.AppSettings["ds"]));   
      string   strSQL="update   material   set   m_shape='"+mytb1.Text+"',m_price="+mytb2.Text+",m_stocks="+mytb3.Text+",p_name='"+mytb4.Text+"'   where   m_num="+DataGrid1.DataKeys[(int)e.Item.ItemIndex];?//取得你要的值。   
      cm=new   OleDbCommand(strSQL,cn);   
      cn.Open();   
      cm.ExecuteNonQuery();   
      cn.Close();   
      DataGrid1.EditItemIndex=-1;   
      getdata();   
      }
      

  8.   

    http://blog.csdn.net/xianfajushi/archive/2008/11/30/3413317.aspx
      

  9.   

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)//信息显示编辑与删除
        {
            pages = Request["page"];
            LinkButton LBut2 = (LinkButton)DataList1.Items[0].FindControl("LinkButton2");
            LinkButton LBut3 = (LinkButton)DataList1.Items[0].FindControl("LinkButton3");
            if (Session["UserName"] != null)
            {
                if (e.CommandName == "BianJi_getID")
                {
                    if (LBut2 != null) { strID = Convert.ToInt16(e.CommandArgument.ToString()).ToString(); }
                    if (TextBox9.Text != "") { Session["BaoChi"] = TextBox9.Text; }
                    Response.Redirect("Gong_Dan_List.aspx?id=" + strID + "&page=" + pages + "");
                }            if (e.CommandName == "ShuanChu_getID")
                {
                    if (LBut3 != null) { strID = Convert.ToInt16(e.CommandArgument.ToString()).ToString(); }
                    ShuanChu_JiLu();
                }
            }
            else
            { Response.Redirect("../default.aspx"); }
        }CheckBox chk = (CheckBox)DataList1.Items[0].FindControl("CheckBox1");Control shousuoKJ = (Control)Master.FindControl("ContentPlaceHolder1"); 
            for (int Bi = 1; Bi <= 3; Bi++) 
            { Button Bton = (Button)shousuoKJ.FindControl("Button" + Bi.ToString()); if (Bton != null) { Bton.Enabled = false; } } LinkButton LBut3 = (LinkButton)Controls.Count[0].FindControl("LinkButton3");----------------------------------------------------------------------------<asp:LinkButton ID="LinkButton3" runat="server" 
                                                                        CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id")%>' 
                                                                        CommandName="GuanLi_getID" Enabled="False" ToolTip="网站管理。">删除</asp:LinkButton>本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xianfajushi/archive/2008/11/30/3413317.aspx