从datagridview中选定行的值的ID中获取名称
Posted
技术标签:
【中文标题】从datagridview中选定行的值的ID中获取名称【英文标题】:To get the name from the ID of the value in the selected row in datagridview 【发布时间】:2021-07-29 19:47:18 【问题描述】:我在 gridview 中隐藏 Author_ID 和 Publisher_ID 。我希望当我单击一行时,作者和发布者组合框文本填充选定的行值。行中有发布者和作者 ID 作为外键,但我想在组合框文本上显示他们的名字。如何访问它们。
private void gridViewBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
gridViewBook.CurrentRow.Selected = true;
txtID.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_ID"].FormattedValue.ToString();
txtISBN.Text = gridViewBook.Rows[e.RowIndex].Cells["ISBN"].FormattedValue.ToString();
txtName.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_NAME"].FormattedValue.ToString();
txtPage.Text = gridViewBook.Rows[e.RowIndex].Cells["PAGE"].FormattedValue.ToString();
txtLang.Text = gridViewBook.Rows[e.RowIndex].Cells["LANGUAGE"].FormattedValue.ToString();
comboBoxAuthor.Text= gridViewBook.Rows[e.RowIndex].Cells["AUTHOR_ID"].FormattedValue.ToString();
comboBoxPublisher.Text = gridViewBook.Rows[e.RowIndex].Cells["PUBLISHER_ID"].FormattedValue.ToString();
我只能访问他们的 ID。 我像这样填充了组合框。
private void fillAuthorBox()
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM AUTHOR ", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboBoxAuthor.ValueMember = "AUTHOR_ID";
comboBoxAuthor.DisplayMember = ("AUTHOR_FULLNAME");
comboBoxAuthor.DataSource = dt;
conn.Close();
private void fillPublisherBox()
conn.Open();
string query = " SELECT * FROM PUBLISHER ";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboBoxPublisher.ValueMember = "PUBLISHER_ID";
comboBoxPublisher.DisplayMember = "PUBLISHER_NAME";
comboBoxPublisher.DataSource = dt;
conn.Close();
【问题讨论】:
不要在任何地方重复使用相同的 SqlConnection (conn
)。它会干扰称为连接池的功能,例如you really are better off creating a new SqlConnection object(在using
块中)对于大多数查询。此外,.Fill()
方法已经为您打开和关闭连接。最后,您将更好地构建数据访问方法以返回数据,而不是直接更新控件。让调用这些方法的代码更新表单中的控件。
您是否考虑在每个文本框中添加DataBinding
?类似于...txtISBN.DataBindings.Add("Text", gridViewBook.DataSource, "ISBN");
...这将消除网格CellContentClick
事件中的所有代码并完成同样的事情。
【参考方案1】:
我首先查看fillAuthorBox()
和fillPublisherBox()
。在某处你有调用这些方法的代码。我们在问题中看不到这一点,所以我假设它是一个通用的表单加载方法,如下所示:
void form_Load(...)
fillAuthorBox();
fillPublisherBox();
我们可以做得更好。这些方法有一些常见的错误,我将在下面更正:
//only re-use the connection string, NOT the whole connection object
private connString As String = "connection string here";
//return a value, don't touch any form controls
// even better if these methods are moved to a different class
public DataTable getAuthorNames()
using (var conn = new SqlConnection(connString))
// Don't SELECT * if you don't need all the fields!
using (var cmd = new SqlCommand("SELECT AUTHOR_ID, AUTHOR_FULLNAME FROM AUTHOR ", conn))
using (var da = new SqlDataAdpater(cmd))
var dt = new DataTable();
da.Fill(dt); // Fill() automatically opens and closes the connection object
return dt;
//using blocks make sure your items are fully disposed when the block closes,
// **even if an exception is thrown!**
public DataTable getPublisherNames()
using (var conn = new SqlConnection(connString))
using (var cmd = new SqlCommand("SELECT PUBLISHER_ID, PUBLISHER_NAME FROM PUBLISHER", conn))
using (var da = new SqlDataAdpater(cmd))
var dt = new DataTable();
da.Fill(dt);
return dt;
现在我原来的 form_Load() 看起来像这样:
void form_Load(...)
comboBoxAuthor.ValueMember = "AUTHOR_ID";
comboBoxAuthor.DisplayMember = "AUTHOR_FULLNAME";
comboBoxAuthor.DataSource = getAuthorNames();
comboBoxPublisher.ValueMember = "PUBLISHER_ID";
comboBoxPublisher.DisplayMember = "PUBLISHER_NAME";
comboBoxPublisher.DataSource = getPublisherNames();
当有人单击网格中的一行时,我们可以从这里继续选择正确的条目。为此,我们不想设置Text
属性,而是要更改选择了哪个项目。由于我们知道ValueItem
,我们可以通过SelectedValue
属性来实现。
private void gridViewBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
gridViewBook.CurrentRow.Selected = true;
txtID.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_ID"].FormattedValue.ToString();
txtISBN.Text = gridViewBook.Rows[e.RowIndex].Cells["ISBN"].FormattedValue.ToString();
txtName.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_NAME"].FormattedValue.ToString();
txtPage.Text = gridViewBook.Rows[e.RowIndex].Cells["PAGE"].FormattedValue.ToString();
txtLang.Text = gridViewBook.Rows[e.RowIndex].Cells["LANGUAGE"].FormattedValue.ToString();
comboBoxAuthor.SelectedValue = gridViewBook.Rows[e.RowIndex].Cells["AUTHOR_ID"].Value;
comboBoxPublisher.SelectedValue = gridViewBook.Rows[e.RowIndex].Cells["PUBLISHER_ID"].Value;
但这里要小心! 我对 WinForms 有点不习惯,而且我不记得这对于不匹配的类型会有多大的宽容度。组合框ValueMember
可能是int
或DataGridViewCell
,但我们使用string
来设置SelectedValue
,这可能效果不佳。您可能必须将字符串转换回 int,或者找到您的值的索引来更新 SelectedIndex 属性。
【讨论】:
非常非常感谢。通常我不会为 Windows 窗体编写代码,但这是我必须做的项目的一部分。由于我对这个主题知之甚少,我需要帮助,尽管这很容易。感谢您的帮助,它正在顺利运行。 @JoelCoehoorn以上是关于从datagridview中选定行的值的ID中获取名称的主要内容,如果未能解决你的问题,请参考以下文章