繁体   English   中英

从另一个类访问 datagridview

[英]Accessing a datagridview from another class

我有一个正在运行的应用程序,但我想通过将我的一些方法移动到它们自己的类中的单独文件中来更好地组织它,因为目前我有一个很长的大型代码列表。 这是我以前没有尝试过的。

我在自己的文件中创建了单独的类并检查了我可以访问它(我可以)但我发现因为它使用主窗体中的 datagridview(原始方法来自)它现在不再可以访问所说的 datagridview来自新班级。

解决这个问题的最佳方法是什么? 我可以将整个 datagridview 作为方法的参数引入,然后访问它吗? 或者我必须一次传递datagridview的每个属性一个参数吗?

该方法基本上也将格式应用于数据网格,因此它也需要能够更新回主表单。

public class GridFormat
    {
        public void applyFormat()
        {
            //Method to Apply formatting to main grid

            int complete = -1;
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                string header = dataGridView1.Columns[i].HeaderText;
                if (header == "Completed" || header == "jlComplete") { complete = i; }
            }

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if ((Convert.ToString(row.Cells[complete].Value) == "True"))
                {
                    row.DefaultCellStyle.ForeColor = Color.Gray;
                }
            }
        }

    }

您可以将数据网格作为参数提供给applyFormat ,或者如果该数据网格特定于您的GridFormat实例,则将其存储在GridFormat的属性中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM