我想将我的datagridview从form1传递到form2。我尝试使用构造函数,但没有结果,第二种形式的datagridview为空。有人可以在这里帮我吗,我被堆积了几个小时。我不使用sql,也不需要使用dataTable.Here是我的代码:
我在datagridview2的cellClick事件上填充了datagridview3。当我单击dayagridview2 cellClick_event时,我的datagridview3填充了以下方法:
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataD d = new DataD();
d.Mat(dataGridView1,dataGridView2,dataGridView3);
}
这是填充dataGridView3的方法:
public void Mat(DataGridView dataGridView1, DataGridView dataGridView2,DataGridView dataGridView3)
{
Named n = new Named();
foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
{
IList<String> lista = new List<String>();
n.Data = string.Empty;
n.Data2 = string.Empty;
int indexOfYourColumn = 9;
int index2 = 0;
var restaurantList = new List<Named>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
n.Data = row.Cells[indexOfYourColumn].Value.ToString();
if (cell.Value.ToString() == n.Data.ToString())
{
restaurantList.Add(new Nalozi()
{
Data = row.Cells[indexOfYourColumn].Value.ToString(),
Data2 = row.Cells[index2].Value.ToString()
});
}
}
dataGridView3.DataSource = restaurantList;
}
}
所以现在我只需要在buttnClick上以另一种形式显示此dataGridView3。
如果你想通过DataGridView
从一种形式到另一种则可能必须通过DataGridView.DataSource
的DataGridView
从一种形式到其他。像这样
new SecondForm(dataGridView.DataSource)
您的SecondForm
遗嘱将接受通过DataSource
并将其传递给DataGridView
该表格
class SecondForm
{
public SecondForm(object dataSource)
{
InitializeComponents();
dataGridView.DataSource = dataSource;
}
}
如果你想传递的副本,DataSource
您可以创建一个新的DataTable
从现有数据中DataGridView
的FirstForm
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}
并更新对此的首次呼叫
new SecondForm(GetDataTableFromDGV(dataGridView))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句