C# 更改一个表格';s整数通过另一种形式

C# 更改一个表格';s整数通过另一种形式,c#,forms,datagridview,C#,Forms,Datagridview,我正在制作一个有DataGridView的程序。 此GridView通过SQL连接通过1个表获取数据。 此表有1列和一些行 对于每一行,都有另一列带有按钮。 当我单击按钮时,我希望它打开另一个表单,它只显示我现在单击的行中的文件名 不幸的是,程序打开表单,但显示表中的第一个单元格。 这是因为Form2从未被告知它应该从Form1获得的e.RowIndex编号,因此它知道要显示哪一行。所以现在它假设无论我点击哪个按钮,我都想要e.rowdexnumber 0,它指的是单元格编号1 那么,关于这个问

我正在制作一个有DataGridView的程序。 此GridView通过SQL连接通过1个表获取数据。 此表有1列和一些行

对于每一行,都有另一列带有按钮。 当我单击按钮时,我希望它打开另一个表单,它只显示我现在单击的行中的文件名

不幸的是,程序打开表单,但显示表中的第一个单元格。 这是因为Form2从未被告知它应该从Form1获得的e.RowIndex编号,因此它知道要显示哪一行。所以现在它假设无论我点击哪个按钮,我都想要e.rowdexnumber 0,它指的是单元格编号1

那么,关于这个问题,; 我如何让它在假定我的chartRowNumber为0之前更新它

表格1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Threading;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {![enter image description here][1]
    DataTable userTable = new DataTable();
    DataGridViewButtonColumn checkMeasurement = new DataGridViewButtonColumn();
    public DataSet ds = new DataSet();
    private SqlConnection con = new SqlConnection(@"Data Source=irrelevant");
    public Form1()
    {
        InitializeComponent();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT BlobFilename FROM tblUsers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        checkMeasurement.HeaderText = "Se Måling";
        checkMeasurement.Text = "";
        checkMeasurement.UseColumnTextForButtonValue = true;
        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.Columns.Add(checkMeasurement);
    }

    public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            Form2 f = new Form2();
            f.chartRowNumber = e.RowIndex;
            Thread.Sleep(1000);
            f.Show();
        }
    }
}
}

表格2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form2 : Form
    {
        public int chartRowNumber;
        public Form2()
        {
            InitializeComponent();
            Form1 f = new Form1();
            DataTable PatientTable = f.ds.Tables[0];
            listBox1.Items.Add(PatientTable.Rows[chartRowNumber].ItemArray[0].ToString());
        }
    }
}
如果您感兴趣,这里有一张GUI的图片。
为接受行号的Form2创建重载构造函数

public int chartRowNumber = 0;
    public Form2()
    {
        InitializeComponent();
        Form1 f = new Form1();
        DataTable PatientTable = f.ds.Tables[0];
        listBox1.Items.Add(PatientTable.Rows[chartRowNumber].ItemArray[0].ToString());
    }

public Form2(int rowIndex)
    {
        InitializeComponent();
        Form1 f = new Form1();
        DataTable PatientTable = f.ds.Tables[rowIndex];
        listBox1.Items.Add(PatientTable.Rows[chartRowNumber].ItemArray[0].ToString());
    }

您必须检查此索引,因为我不确定您要在何处使用
rowIndex
变量,但您可以使用此方法将其发送到
Form2
实例

为接受行号的Form2创建重载构造函数

public int chartRowNumber = 0;
    public Form2()
    {
        InitializeComponent();
        Form1 f = new Form1();
        DataTable PatientTable = f.ds.Tables[0];
        listBox1.Items.Add(PatientTable.Rows[chartRowNumber].ItemArray[0].ToString());
    }

public Form2(int rowIndex)
    {
        InitializeComponent();
        Form1 f = new Form1();
        DataTable PatientTable = f.ds.Tables[rowIndex];
        listBox1.Items.Add(PatientTable.Rows[chartRowNumber].ItemArray[0].ToString());
    }

您必须检查此索引,因为我不确定您要在何处使用
行索引
变量,但您可以使用此方法将其发送到
Form2
实例

由于两个原因,您的代码无法工作。第一个原因是因为Form2的构造函数在传递rowIndex之前运行,第二个原因是因为您构建了Form1的另一个实例来提取datatable。第二个实例有自己的gridview,Form1的构造函数重新查询数据库并填充第二个Form1实例的gridview。在本例中,dataGridView1_CellContentClick事件从不触发,并且您没有任何与零不同的行索引

在CellContentClick事件中,您需要向Form2构造函数传递构造函数中所需的行索引和表

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        Form2 f = new Form2(e.RowIndex, ds.Tables[0]);
        f.Show();
    }
}
在Form2构造函数中,使用从Form1的当前实例传递的两个变量,而不构建另一个实例

public partial class Form2 : Form
{
    public Form2(int chartRowNumber, DataTable form1Table)
    {
        InitializeComponent();
        listBox1.Items.Add(form1Table.Rows[chartRowNumber].ItemArray[0].ToString());
    }
}
当然,如果您需要为其他操作保留这些变量,您应该将它们保存在Form2当前实例中的全局变量中

相反,如果在form2中,您不需要rowNumber和datatable,那么您可以直接从Form1的当前实例传递项数组的元素零

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        Form2 f = new Form2(ds.Tables[0].Rows[e.RowIndex][0].ToString());
        f.Show();
    }
}

public partial class Form2 : Form
{
    public Form2(string item)
    {
        InitializeComponent();
        listBox1.Items.Add(item);
    }
}

您的代码无法工作有两个原因。第一个原因是因为Form2的构造函数在传递rowIndex之前运行,第二个原因是因为您构建了Form1的另一个实例来提取datatable。第二个实例有自己的gridview,Form1的构造函数重新查询数据库并填充第二个Form1实例的gridview。在本例中,dataGridView1_CellContentClick事件从不触发,并且您没有任何与零不同的行索引

在CellContentClick事件中,您需要向Form2构造函数传递构造函数中所需的行索引和表

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        Form2 f = new Form2(e.RowIndex, ds.Tables[0]);
        f.Show();
    }
}
在Form2构造函数中,使用从Form1的当前实例传递的两个变量,而不构建另一个实例

public partial class Form2 : Form
{
    public Form2(int chartRowNumber, DataTable form1Table)
    {
        InitializeComponent();
        listBox1.Items.Add(form1Table.Rows[chartRowNumber].ItemArray[0].ToString());
    }
}
当然,如果您需要为其他操作保留这些变量,您应该将它们保存在Form2当前实例中的全局变量中

相反,如果在form2中,您不需要rowNumber和datatable,那么您可以直接从Form1的当前实例传递项数组的元素零

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        Form2 f = new Form2(ds.Tables[0].Rows[e.RowIndex][0].ToString());
        f.Show();
    }
}

public partial class Form2 : Form
{
    public Form2(string item)
    {
        InitializeComponent();
        listBox1.Items.Add(item);
    }
}

谢谢这正是我想要的。谢谢。这正是我想要的。