访问单元格dataGridView(表单1)的值到表单2,导致索引超出范围

时间:2015-05-27 18:46:50

标签: c# vb.net forms winforms datagridview

我在表单1中公开了我的dataGridView,我可以从第二个表单访问其HeaderText但我无法访问dataGridView的任何其他属性/值。 我通过int gridLoopCount = dataGridView1.Rows.Count-1;

从Form1获取gridLoopCount的值

所以,我的循环工作,我通过更改我的图形字符串e.Graphics.DrawString("my value"......

来尝试它

以下是表单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++;
            }

0 个答案:

没有答案