2

我正在尝试使用数据网格的内容填充文本框。

我正在使用一个名为 XYZCompany.accbd 的数据源。我用数据库的内容填充 dgv,在这种情况下,包含供应商 ID(自动编号字段)、供应商名称、供应商地址、电话号码和合同名称。

首先,我用这些字段填充 dgv。然后我正在努力的下一步是,当我单击 dgv 中的一行时,它应该在文本框中显示该行的数据。问题是当我单击 dgv 行时,文本框中没有显示任何内容,并且我没有收到错误。

我将我的连接作为参数传递给新表单。但无论如何我都会指定它。这是我通过的声明:

    OleDbConnection dbConn;
    OleDbCommand dbCmd;
    DataSet ds = new DataSet();
    OleDbDataAdapter dbAdapter;
    private void ViewAllSuppliers_Load(object sender, EventArgs e)
    {
        dbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data     Source=XYZCompany.accdb");

        dbCmd = new OleDbCommand("SELECT * FROM Supplier ORDER BY [Supplier ID]", dbConn);


        dbAdapter = new OleDbDataAdapter(dbCmd);
        dbAdapter.Fill(ds, "Suppliers");

    }

    private void btnViewSuppliers_Click(object sender, EventArgs e)
    {
        dgvSuppliers.DataSource = ds.Tables["Suppliers"];
    }

那是我传递的参数的代码。

这是我试图用来填充文本框的表单中的代码:

public partial class ChangeSupplier : Form
{
    OleDbConnection dbConn;
    OleDbCommand dbCmd;
    DataSet ds;
    OleDbDataAdapter dbAdapter;
    OleDbDataReader dbReader;

    public ChangeSupplier(OleDbConnection dbConn, OleDbCommand dbCmd, DataSet ds, OleDbDataAdapter dbAdapter)
    {
        InitializeComponent();
        this.dbConn = dbConn;
        this.dbCmd = dbCmd;
        this.ds = ds;
        this.dbAdapter = dbAdapter;
    }

    private void ChangeSupplier_Load(object sender, EventArgs e)
    {
        dgvSuppliers.DataSource = ds.Tables["Suppliers"];
    }

    private void dgvSuppliers_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dgvSuppliers.SelectedRows.Count > 0)
        {

            dbConn.Open();

            dbCmd = new OleDbCommand("SELECT * FROM Supplier WHERE [Supplier ID] = '" + dgvSuppliers.SelectedRows[0].Cells[0].Value + "'", dbConn);

            dbReader = dbCmd.ExecuteReader();

            while (dbReader.Read())
            {

                txtName.Text = dbReader["[Supplier Name]"].ToString();
                txtTelNo.Text = dbReader["[Telephone No]"].ToString();
                txtAddress.Text = dbReader["[Supplier Address]"].ToString();
                txtContactName.Text = dbReader["[Contact Name]"].ToString();

            }
        }
    }
}

请随时提出任何建议...在此先感谢

4

2 回答 2

2

以下是上述问题的解决方案:

    private void dgvSuppliers_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {

            DataGridViewRow row = this.dgvSuppliers.Rows[e.RowIndex];

            txtID.Text = row.Cells["Supplier ID"].Value.ToString();
            txtName.Text = row.Cells["Supplier Name"].Value.ToString();
            txtTelNo.Text = row.Cells["Telephone No"].Value.ToString();
            txtAddress.Text = row.Cells["Supplier Address"].Value.ToString();
            txtContactName.Text = row.Cells["Contact Name"].Value.ToString();

        }

    }

我使用行索引来查找用户单击的单元格。

于 2013-06-08T19:43:14.787 回答
1
private void dgvSuppliers_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {

        DataGridViewRow row = this.dgvSuppliers.Rows[e.RowIndex];

        txtID.Text = row.Cells["Supplier ID"].Value.ToString();
        txtName.Text = row.Cells["Supplier Name"].Value.ToString();
        txtTelNo.Text = row.Cells["Telephone No"].Value.ToString();
        txtAddress.Text = row.Cells["Supplier Address"].Value.ToString();
        txtContactName.Text = row.Cells["Contact Name"].Value.ToString();

    }

}
于 2013-09-29T18:27:57.793 回答