无法在Form1.cs中访问dataGridView1

问题描述:

我正在尝试从数据网格读取数据
所以我建议使用这样的东西



I was trying to work read data from data grids
So i got a suggestion to use something like this



foreach (DataGridViewRow DataRow in mydataGridView1.Rows)
{

    foreach (DataGridViewCell DataCell in DataRow.Cells)
    {
          //my other code
    }
}



所以我将mydataGridView1定义为局部变量.但这行不通.
所以我使用定义System.Windows.Forms.DataGridView dataGridView1;存在于InitializeComponent()中.但是我不认为这是一个永久的解决方案.



so i definded mydataGridView1 as a local variable. but this wont work.
So i use the definition System.Windows.Forms.DataGridView dataGridView1; present in InitializeComponent(). but that i dont think is a permanent solution. Can any one provide me how to do this.

听起来像是您开始编写代码甚至都没有阅读基础文章的声音!

例如,如果要检索第一行的第一列,则可以尝试:
Sounds like you started coding without even reading the basic articles!

For example, if you want to retrieve the first column of the first row, you can try:
MyGridView.Rows[0].Cells[0].Value;



现在,在编码之前先阅读以下内容:
DataGridView类 [使用DataGrid控件 [ C#DataGridView教程 [ C#DataGridView提示和秘密 [ ^ ]
如何:向Windows窗体DataGridView控件中的单个单元格添加工具提示 [ ^ ]



Now, have a read on these before coding:
DataGridView Class[^]
Using the DataGrid Control[^]
C# DataGridView Tutorial[^]
C# DataGridView Tips and Secrets[^]
How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control[^]


对不起,我想当我给您解决方案时很明显,您应该用您正在使用的DataGridView的名称替换mydataGridView1在您的表格上.

如果我知道您无法自己解决该问题,那么我将其称为"dontUseThisNameButUseTheNameOfTheDataGridViewOnYourFormCommaYouKnowCommaTheOneYouAreTryingToGetTheDataFrom".
所以:
I''m sorry, I thought it was obvious when I gave you the solution, that you should replace mydataGridView1 with the name of the DataGridView you were using on your form.

If I had known you couldn''t work that out for yourself, I would have called it "dontUseThisNameButUseTheNameOfTheDataGridViewOnYourFormCommaYouKnowCommaTheOneYouAreTryingToGetTheDataFrom" instead.
So:
foreach (DataGridViewRow row in dontUseThisNameButUseTheNameOfTheDataGridViewOnYourFormCommaYouKnowCommaTheOneYouAreTryingToGetTheDataFrom.Rows)
    {
    foreach (DataGridViewCell thisIsTheCellFromTheDataGridViewRowThatYouAreTryingToFind in row.Cells)
        {
        ...
        }
    }

对于由此给您带来的任何不便,我深表歉意.

My apologies for any inconvenience caused.