日期:2014-05-17  浏览次数:20800 次

DataGridView 绑定了DataTable 数据 我加了一列DataGridViewCheckBoxColumn 如何根据选中的生成新的DT
DataGridView 绑定了DataTable 数据 比如有10列 我只显示其中5列 设定了AutoGenerateColumns = false;

第一列加了DataGridViewCheckBoxColumn  让客户勾选选择想要的数据

问题是如何根据选中记录的生成新的DT


                DataSet newds = new DataSet();
                DataTable dt = oldds.Tables[0].Clone();

                foreach (DataGridViewRow row in this.dgvViewRow.Rows)
                {
                    if ((bool)row.Cells["colCheck"].Value) // 如果是选中
                    {
                        dt.Rows.Add((row.DataBoundItem as DataRowView)); //这里一直报错
                    }
                }
 

------解决方案--------------------
           dgvUserManag.DataSource = dtUser;         
            DataTable dt = dtUser.Clone();
            foreach (DataGridViewRow row in this.dgvUserManag.Rows)
            {
                Boolean chk = Boolean.Parse(dgvUserManag.Rows[i].Cells[0].EditedFormattedValue.ToString());

                if (chk) // 如果是选中
                {
                    
                    dt.Rows.Add((row.DataBoundItem as DataRowView).Row.ItemArray); //这里一直报错

                }
            }
百度报错的内容,找解决方案