Andre Smith
Andre Smith

Reputation: 207

Clicking on a datagrid row and displaying its contents in textboxes

I am trying to populate textboxes with the content of a data grid.

I am using a data source named XYZCompany.accbd. I populate the dgv with the content of the database, In this case containing a supplier ID(auto number field), a supplier name, supplier address, telephone no and a contract name.

Firstly I fill the dgv with these fields. Then the next step I am strugling with is that when I click on a row in the dgv, it should display the data of the row in the textboxes. The problem is when i click on the dgv row, nothing is diplayed in the text boxes, and I receive no error.

I pass my connection as a parameter to the new form. But i will specify it anyway. Here is the declarations i pass:

    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"];
    }

That is the code for the parameters I pass.

Here is the code I have in the form that i am trying to use to populate the textboxes:

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();

            }
        }
    }
}

Please feel free to suggest anything... Thanks in advance

Upvotes: 2

Views: 4081

Answers (2)

aceyouth
aceyouth

Reputation: 11

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();

    }

}

Upvotes: 1

Andre Smith
Andre Smith

Reputation: 207

Here is the solution to the problem above:

    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();

        }

    }

I used the row index to find the cell that the user clicks.

Upvotes: 2

Related Questions