c# - 从datagridview中选定行的值的ID中获取名称
问题描述
我在 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();
}
解决方案
我在看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
or DataGridViewCell
,但我们使用 astring
来设置SelectedValue
, 可能效果不佳。您可能必须将字符串转换回 int,或者找到值的索引来更新 SelectedIndex 属性。
推荐阅读
- go - 在 Couchbase 中插入只读文档
- docker - 如何停止docker自动启动jupyter笔记本
- angular - 我使用 canDeactive 时如何使用 Skiplocation
- java - 在 Java 中使用下一行打印字符串(斑马线模式)
- google-chrome - 与 http://local ip/ 上的跨站点资源关联的 cookie 被设置为没有 `SameSite` 属性
- wordpress - 如何确保在 Wordpress 中进行缓存
- django - 获取验证错误字段不能为空,即使模型中的 null=True
- python - 如何在 MIB 树中添加变量?
- node.js - 将通过 graphQL 公开的单个微服务连接到应用程序网关聚合层
- python - keras CNN model.predict_classes 返回 3 个元素的数组