从另一个表单访问datagrid值

时间:2014-06-24 17:13:25

标签: c# forms datagridview

我想从另一个表单访问datagrid值。我该怎么办?

我在父表单中使用此方法,但它不起作用:

    public static int indicebis(DataGridView dataGridView1)
    {
        string titRicerca = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString().ToUpper();
        string datRicerca = dataGridView1.CurrentRow.Cells["Column4"].Value.ToString().ToUpper();
        int x = 0;
        for (int i = 0; i < DBList.Count; i++)
        {
            if (DBList[i].Titolo.ToUpper() == titRicerca && DBList[i].Data.ToUpper() == datRicerca)
            {
                x = i;
            }
        }
        return x;
    }

1 个答案:

答案 0 :(得分:0)

我缺少更多代码我将为您提供一个通用公式,用于将数据从一个表单提供给另一个表单。假设您通过按钮单击父表单来调用第二个表单:

    private void button1_Click(object sender, EventArgs e)
    {
        using (SecondForm form = new SecondForm())
        {
            form.titRicerca = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString().ToUpper();
            form.datRicerca = dataGridView1.CurrentRow.Cells["Column4"].Value.ToString().ToUpper();
            form.ShowDialog();

            // after you have exited the second form, form.titRicerca and form.datRicerca will
            // still be available for as long as you are in this using clause.  Assign them
            // elsewhere if you need the parent form to access them
        }
    }

您的辅助表单将包含以下公共变量:

public partial class SecondForm : Form
{
    public string titRicerca = null;
    public string datRicerca = null;
}