日期:2014-05-20 浏览次数:21005 次
for(int i=0;i<dgvA.selectRows.Count;i++)
{
DataGridViewRow dgvRow=dgv.selectRows[i];
dgvB.Rows.remove(dgvRow);//如果选中行有多个这时移除不完全,比如选中了三个只移除了俩,为什么???该怎么做呢。
dgvB.Rows.Add(dgvRow);//这里添加的数据在dgvB中不显示。
}
Page
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns = "false">
<Columns>
<asp:BoundField DataField = "Id" HeaderText ="ID" />
<asp:BoundField DataField = "Name" HeaderText ="Name" />
<asp:BoundField DataField ="Age" HeaderText = "Age" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "btnAdd_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
Model
[Serializable]
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow row = btn.Parent.Parent as GridViewRow;
if (row == null) return;
Student s = new Student();
s.Id = int.Parse(row.Cells[0].Text);
s.Name = row.Cells[1].Text;
s.Age = int.Parse(row.Cells[2].Text);
List<Student> students = this.AddStudent;
students.Add(s);
this.GridView2.DataSource = students;
this.GridView2.DataBind();
this.AddStudent = students;
}
public List<Student> AddStudent
{
get
{
if (this.ViewState["Students"] == null) return new List<Student>();
return this.ViewState["Students"] as List<Student>;
}
set
{
this.ViewState["Students"]= value;
}
}
}
------解决方案--------------------
DataGridViewRow[] drs=new DataGridViewRow[dataGridView1.SelectedRows.Count];
dataGridView1.SelectedRows.CopyTo(drs, 0);
for (int i = 0; i < drs.Length; i++)
{
//这是完整的添加
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dataGridView2);
dr.CreateCells(dataGridView2);
//也可以只部分添加
//只需要改变这个的赋值项
dr.Cells[0].Value = drs[i].Cells[0].Value;
dr.Cells[1].Value = drs[i].Cells[1].Value;
dataGridView2.Rows.Add(dr);
}
------解决方案--------------------
object[] ob = new object[] { dgv1.CurrentRow.Cells["itemName"].Value.ToString(),
dgv1.CurrentRow.Cells["ITEMM_MID"].Value.ToString(), "", "",
dgv1.CurrentRow.Cells["checkup_price"].Value.ToString()
};
//添加一行数据
dgv2.Rows.Add(ob);
//写个数组~
------解决方案--------------------
dgvB.Rows.remove(dgvRow);
dgvB.Rows.Add(dgvRow);
这样本身不对吧。dgvRow是第一个gridview的引用,直接加到第二个对象肯定不行,而且你先remove了,再去加,应该加的是个空对象吧。建议remove之前在dgvB新建一个row,再把dgvRow深拷贝过来,再去remove第一个。