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

DataGridView批量修改数据

DataGridView 数据用List绑定的,在DataGridView第一列有个复选框,现在要将复选框选中的行数据批量修改?如何获得选中行的数据,并对数据进行批量的修改。有示例代码参考参考 --------------------编程问答-------------------- 判断DataGridView 是否有选择的项,更改值,保存DataGridView 
for(循环DataGridView 的值)
{
 if(判断是否选中)
{
改值
}
}
保存DataGridView  --------------------编程问答--------------------
 foreach (DataGridViewRow dgvr in dgvBox.Rows)
            {
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgvr.Cells[0];
                bool flag = Convert.ToBoolean(checkCell.Value);
                if (flag)//如果此项被选中
                {
                    string oid = dgvr.Cells[1].Value.ToString();
                    Ocean_OutStockRoom oos =new Ocean_OutStockRoom();
                    oos.OOSRID = int.Parse(oid);
                    oos.CheckPerson =Logon.CurrentUser.Name;
                    Ocean_OutStockRoomManager.ModifyOcean_OutStockRoomByOsidAndCheckPerson(oos,Logon.CurrentUser);
                }
            }

里面有类是自己的,改成你的行了 --------------------编程问答--------------------

for(int i = 0; i < this.dgvPayStyle.Rows.Count; i++)
{
 if(this.dgvPayStyle.Rows[i].Cells["你复选框的列名"].EditedFormattedValue.ToString()== "True")
{
  //做你要做的操作。  
}
}

--------------------编程问答--------------------
    protected void btnOK_Click(object sender, ImageClickEventArgs e)
    {
        string jscript = string.Empty;
        DataSet myDS = new DataSet();
        myDS.Tables.Add("RecvInfo");
        myDS.Tables[0].Columns.Add("GiftsID");
        myDS.Tables[0].Columns.Add("ShopID");
        myDS.Tables[0].Columns.Add("BeforeNum");
        myDS.Tables[0].Columns.Add("CheckNum");
        myDS.Tables[0].Columns.Add("CreateDate");
        myDS.Tables[0].Columns.Add("CreateUser");
        myDS.Tables[0].Columns.Add("CheckType");
        for (int i = 0; i < gvCheck.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvCheck.Rows[i].FindControl("chkSel");
            if (ck.Checked)
            {
                if (((TextBox)gvCheck.Rows[i].Cells[3].FindControl("txtNum")).Text.Trim() == string.Empty)
                {
                    jscript = "alert('请填写数量!');";
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "onclick", jscript, true);
                    return;
                }
                try
                {
                    int Count = int.Parse(((TextBox)gvCheck.Rows[i].Cells[3].FindControl("txtNum")).Text);
                    int GiftsID = Util.GetInt(((Label)gvCheck.Rows[i].FindControl("lbGiftsID")).Text);
                    int GiftsNum = Util.GetInt(((Label)gvCheck.Rows[i].FindControl("lblGiftsNum")).Text);

                    DataRow myRow = myDS.Tables[0].NewRow();
                    myRow["GiftsID"] = GiftsID;
                    myRow["ShopID"] = Util.GetInt(Util.GetCookie("ShopID")); ;
                    myRow["BeforeNum"] = GiftsNum;
                    myRow["CheckNum"] = Count;
                    myRow["CreateDate"] = DateTime.Now;
                    myRow["CreateUser"] = Security.Decrypt(Util.GetCookie("ManageUserID"));
                    myRow["CheckType"] = 1;
 
                    myDS.Tables[0].Rows.Add(myRow);
                }
                catch (Exception ee)
                {
                    jscript = "alert('" + ee.Message.ToString() + "');";
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "onclick", jscript, true);
                    return;
                }
            }
        }
        if (myDS.Tables[0].Rows.Count == 0)
        {
            jscript = "alert('请正确勾选的东东!');";
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "onclick", jscript, true);
            return;
        }        
        if (bll.AddCheckStore(myDS, Util.GetInt(Util.GetCookie("ShopID")), 3))
        {
            jscript = "alert('成功提交了 " + myDS.Tables[0].Rows.Count + " 条记录!');";
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "onclick", jscript, true);
            FillGifts();
        }
        else
        {
            jscript = "alert('失败!');";
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "onclick", jscript, true);
        }
    }
    /// <summary>
    /// CheckBox勾选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked)
        {
            for (int i = 0; i < gvCheck.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvCheck.Rows[i].FindControl("chkSel");
                if (ck.Enabled == true)
                    ck.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < gvCheck.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvCheck.Rows[i].FindControl("chkSel");
                ck.Checked = false;
            }
        }
    }
--------------------编程问答-------------------- --------------------编程问答-------------------- 上面已经给出答案了 --------------------编程问答-------------------- 谢谢各位大虾,问题解决了 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 4 楼 shine_fly 的回复:
C# code
    protected void btnOK_Click(object sender, ImageClickEventArgs e)
    {
        string jscript = string.Empty;
        DataSet myDS = new DataSet();
        myDS.Tables.Add("RecvInfo")……

好。。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,