点击一个DataGrid行,并在文本框中显示的内容 [英] Clicking on a datagrid row and displaying its contents in textboxes
问题描述
我试图来填充数据网格的内容文本框。
I am trying to populate textboxes with the content of a data grid.
我使用一个名为XYZCompany.accbd的数据源。我填充数据库的内容的DGV,在包含一个供应商的ID(自动号码字段),一个供应商名称,供应商地址,电话不和一个合同名这种情况
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.
首先,我填补这些领域DGV。那么下一步我与strugling是,当我在DGV点击一行时,它应在文本框显示该行的数据。问题是,当我在DGV行点击,没有什么是diplayed在文本框中,我会收到任何错误。
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
推荐答案
下面是解决上述问题:
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.
这篇关于点击一个DataGrid行,并在文本框中显示的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!