如何将一个窗体中datagridview选定的多行信息添加到另一窗体的datagridview中
现有窗体FROM1中有datagridview1,窗体FROM2中有datagridview2,其中datagridview2绑定数据源,请问大家该如何实现将datagr...
现有窗体FROM1中有datagridview1,窗体FROM2中有datagridview2,其中datagridview2绑定数据源,请问大家该如何实现将datagridview2中选定的多行信息添加到datagridview1中?麻烦大家能够给出详细的实现代码,谢谢大家
展开
展开全部
给你个思路吧。在form2写个公共的方法来操作FROM2.datagridview2
在 form1中选中的记录。可以用
ArrayList index = new ArrayList();
for (int i = 0; i < this.iGridView1.Rows.Count; i++)
{
if (iGridView1.Rows[i].Selected == true)
{
index.Add(iGridView1.Rows[i].Index);
}
}
把所有选中的记录添加到对象数组中。调用form2的公共方法,就很简单的把选中记录添加到datagridview2中了
在 form1中选中的记录。可以用
ArrayList index = new ArrayList();
for (int i = 0; i < this.iGridView1.Rows.Count; i++)
{
if (iGridView1.Rows[i].Selected == true)
{
index.Add(iGridView1.Rows[i].Index);
}
}
把所有选中的记录添加到对象数组中。调用form2的公共方法,就很简单的把选中记录添加到datagridview2中了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public Class FormInfo
Private Sub ButtonCopy_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonCopy.Click
If DataGridViewSource.SelectedRows.Count = 0 Then
'如果没有选择行,则不允许打开.
Exit Sub
Else
Dim NameList As List(Of String) = New List(Of String)
For itempa As Integer = 0 To DataGridViewSource.Columns.Count - 1
NameList.Add(DataGridViewSource.Columns(itempa).HeaderText)
Next
建立一个表的名称列表.
Dim SetValues As List(Of List(Of Object)) = New List(Of List(Of Object))
For itempa As Integer = 0 To DataGridViewSource.SelectedRows.Count - 1
Dim NewItem As List(Of Object) = New List(Of Object)
For itempb As Integer = 0 To DataGridViewSource.Columns.Count - 1
NewItem.Add(DataGridViewSource.SelectedRows(itempa).Cells(itempb).Value)
Next
每次徨一行,建立一个值的列表.
SetValues.Add(NewItem)
Next
If FormShow.Visible = False Then
FormShow.Show()
End If
FormShow.AddLines(NameList, SetValues)
End If
End Sub
End Class
'====================
Public Class FormShow
Public Sub AddLines(ByVal SetNames As List(Of String), _
ByVal SetValueRows As List(Of List(Of Object)))
DataGridViewDest.Columns.Clear()
For itempa As Integer = 0 To SetNames.Count - 1
DataGridViewDest.Columns.Add(SetNames(itempa) & itempa.ToString, SetNames(itempa))
Next
先将列清除,再将列名称添加进来.使数据一致.
For Each ss As List(Of Object) In SetValueRows
DataGridViewDest.Rows.Add(ss.ToArray)
将数据一行一行地添加进来.
注意,这里使用了LIST传递数据,而使用ARRAY导入数据.是因为个人爱好.
Next
End Sub
End Class
Private Sub ButtonCopy_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonCopy.Click
If DataGridViewSource.SelectedRows.Count = 0 Then
'如果没有选择行,则不允许打开.
Exit Sub
Else
Dim NameList As List(Of String) = New List(Of String)
For itempa As Integer = 0 To DataGridViewSource.Columns.Count - 1
NameList.Add(DataGridViewSource.Columns(itempa).HeaderText)
Next
建立一个表的名称列表.
Dim SetValues As List(Of List(Of Object)) = New List(Of List(Of Object))
For itempa As Integer = 0 To DataGridViewSource.SelectedRows.Count - 1
Dim NewItem As List(Of Object) = New List(Of Object)
For itempb As Integer = 0 To DataGridViewSource.Columns.Count - 1
NewItem.Add(DataGridViewSource.SelectedRows(itempa).Cells(itempb).Value)
Next
每次徨一行,建立一个值的列表.
SetValues.Add(NewItem)
Next
If FormShow.Visible = False Then
FormShow.Show()
End If
FormShow.AddLines(NameList, SetValues)
End If
End Sub
End Class
'====================
Public Class FormShow
Public Sub AddLines(ByVal SetNames As List(Of String), _
ByVal SetValueRows As List(Of List(Of Object)))
DataGridViewDest.Columns.Clear()
For itempa As Integer = 0 To SetNames.Count - 1
DataGridViewDest.Columns.Add(SetNames(itempa) & itempa.ToString, SetNames(itempa))
Next
先将列清除,再将列名称添加进来.使数据一致.
For Each ss As List(Of Object) In SetValueRows
DataGridViewDest.Rows.Add(ss.ToArray)
将数据一行一行地添加进来.
注意,这里使用了LIST传递数据,而使用ARRAY导入数据.是因为个人爱好.
Next
End Sub
End Class
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询