datagrid中我用模板列绑定了多个checkbox,当我选中其中某一个是如何知道我选中的是那一行的那个,我要把更改后的值自动更新到数据库怎么做,一直无法获取点的是那行的checkbox,一个晚上没有解决,急,高手帮帮忙!!!

解决方案 »

  1.   

    int startIndex = 0;
                CheckBox cbProcessed = new CheckBox();
                foreach(GridViewRow rows in AllAppointmentsGridView.Rows)
                {
                    cbProcessed = (CheckBox)rows.FindControl("newcbProcessed");
                    if(cbProcessed.Checked)
                    {
                        startIndex = rows.DataItemIndex
                        DataKey keyValue = AllAppointmentsGridView.DataKeys[startIndex];
                        string clientId = keyValue[0].ToString();
                        Int32 hubId = Convert.ToInt32(keyValue[1]);
                        Int32 appointmentId = Convert.ToInt32(keyValue[2]);
                        Int32 doctorTypeID = Convert.ToInt32(keyValue[3]);
                        cswAppointments.BFInsertDataToCalendar(hubId, appointmentId, this.UserID, this.viewTime, this.insertTime, doctorTypeID, clientId);//存储过程                }            }
      

  2.   

    http://www.cnblogs.com/ilovejolly/archive/2006/10/24/538120.html
      

  3.   

    foreach (DataGrid gvr in DataGrid1.Rows)
            {
                CheckBox cb = (CheckBox)gr.FindControl("CheckBox1");
                if (cb.Checked)
    这几句话是重点
    按行遍历DataGrid1
    取出每一行的checkbox
    判断是否被选中
      

  4.   

    1楼的正确!
    支持lxmfll2000(lxm)
      

  5.   

    多谢大家帮忙,我的问题还没有解决,我就没有放任何按钮,不能执行按钮事件,我的意思是点一下datagrid中的checkbox,数据库中自动更新,我的代码如下:
    private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Item)
    {
       CheckBox chk = (CheckBox)e.Item.FindControl("cb");
       chk.CheckedChanged +=new EventHandler(chk_CheckedChanged);
    }
    }private void chk_CheckedChanged(object sender, System.EventArgs e)

    //就是不知道当点中一个CheckBox时这个事件中怎么获得是点中的那一个。
                      }一个很令我头痛的问题,请各位高手帮帮忙。
      

  6.   

    我借这个问题来推销一下asp.net控件的“简易扩展”的功效:public class myCheckBox: CheckBox
    {
      [DefaultValue(null)]
      public string datakey
      {
        get{ return (string)ViewState["datakey"]; }
        set{ ViewState["datakey"]=value; }
      }
    }然后你的工程编译之后,你的控件工具箱里就有了这个自定义的控件myCheckBox了。之后在你的datagrid的模板列里不要使用CheckBox,使用myCheckBox,并且为其datakey绑定某个数据(例如绑定当前数据行的key值)。asp.net是个自顶向下把组件式开发技术设计得很周全的操作系统,绑定表达式、组件的继承层次、ItTemplate等扩展技术都是相辅相成的。最后,在chk_CheckedChanged中使用
      ((myCheckBox)object).datakey
    就能取出当初绑定的数据了。asp.net控件是很简单的,多多学习控件知识,从原有的控件上扩展功能。