C# 将dataGridView(表格1)的单元格值访问到表格2中,导致索引超出范围
我已经在表单1中公开了我的dataGridView,我可以从第二个表单访问它的C# 将dataGridView(表格1)的单元格值访问到表格2中,导致索引超出范围,c#,vb.net,forms,winforms,datagridview,C#,Vb.net,Forms,Winforms,Datagridview,我已经在表单1中公开了我的dataGridView,我可以从第二个表单访问它的HeaderText,但我无法访问dataGridView的任何其他属性/值。 我通过int gridLoopCount=dataGridView1.Rows.Count-1从Form1中获取gridLoopCount的值 因此,我的循环工作正常,我通过更改我的图形字符串e.graphics.DrawString(“我的值”… 这是表格2的代码: //Drawing the data grid
HeaderText
,但我无法访问dataGridView
的任何其他属性/值。
我通过int gridLoopCount=dataGridView1.Rows.Count-1从Form1中获取gridLoopCount的值代码>
因此,我的循环工作正常,我通过更改我的图形字符串e.graphics.DrawString(“我的值”…
这是表格2的代码:
//Drawing the data grid
Recieve_Donation_Stock rds = new Recieve_Donation_Stock();
e.Graphics.DrawString(label7.Text, label7.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label7.Location.X), (tableLayoutPanel2.Location.Y + label7.Location.Y));
e.Graphics.DrawString(rds.dataGridView1.Columns[1].HeaderText, label9.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label9.Location.X), (tableLayoutPanel2.Location.Y + label9.Location.Y));
e.Graphics.DrawString(rds.dataGridView1.Columns[2].HeaderText, label10.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label10.Location.X), (tableLayoutPanel2.Location.Y + label10.Location.Y));
int j = 1;
for (int i = 0; i <=gridLoopCount; i++)
{
e.Graphics.DrawString(i.ToString(), label13.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label7.Location.X), (tableLayoutPanel2.Location.Y + label7.Location.Y + (15*j)));
e.Graphics.DrawString(rds.dataGridView1.Rows[i].Cells[0].Value.ToString(), label13.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label9.Location.X), (tableLayoutPanel2.Location.Y + label9.Location.Y + (15 * j)));
e.Graphics.DrawString(rds.dataGridView1.Rows[i].Cells[1].Value.ToString(), label13.Font, Brushes.Black, (tableLayoutPanel2.Location.X + label10.Location.X), (tableLayoutPanel2.Location.Y + label10.Location.Y + (15 * j)));
j++;
}
//绘制数据网格
接收捐赠股票rds=新接收捐赠股票();
e、 Graphics.DrawString(label7.Text,label7.Font,brush.Black,(tableLayoutPanel2.Location.X+label7.Location.X),(tableLayoutPanel2.Location.Y+label7.Location.Y));
e、 Graphics.DrawString(rds.dataGridView1.Columns[1].HeaderText,label9.Font,Brush.Black,(tableLayoutPanel2.Location.X+label9.Location.X),(tableLayoutPanel2.Location.Y+label9.Location.Y));
e、 Graphics.DrawString(rds.dataGridView1.Columns[2].HeaderText,label10.Font,Brush.Black,(tableLayoutPanel2.Location.X+label10.Location.X),(tableLayoutPanel2.Location.Y+label10.Location.Y));
int j=1;
对于(int i=0;i在第二个表单上,从功能上看,您到底想要什么?表单2上的datagridview
是否应该反映与表单1上的datagridview
相同的数据?@alybaba726我正在打印datagridview
,方法是将数据放在新表单上,甚至在form\u load上调用print我们的代码,我只是想弄明白你为什么要调用print。是gridLoopCount
在Form1
还是Form2
?如果是Form1
,那么Form2
是怎么知道的?