【发布时间】:2021-12-12 18:43:13
【问题描述】:
我的代码如下,我无法从数据表中获取值以显示在主窗体的文本框中。我最近意识到 DataGridView 与 DataTable 不同。我认为这让我更接近了。我最初在获取特定索引时遇到问题,因为数据表不能那样工作。现在我正在使用 datagridview,我觉得我几乎在那里,但缺少一些愚蠢的东西。
我的代码
private void dt_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
if (dgv.SelectedCells.Count > 0)
{
active_Description.Text =
Convert.ToString(dgv.CurrentRow.Cells["Description"].Value);
active_Cost.Text = Convert.ToString(dgv.CurrentRow.Cells["Cost"].Value);
active_Diameter.Text = Convert.ToString(dgv.CurrentRow.Cells["Diameter"].Value);
active_Last_Used.Text = Convert.ToString(dgv.CurrentRow.Cells["CareTaker"].Value);
active_Location.Text = Convert.ToString(dgv.CurrentRow.Cells["Location"].Value);
active_OAL.Text = Convert.ToString(dgv.CurrentRow.Cells["OAL"].Value);
active_Vendor.Text = Convert.ToString(dgv.CurrentRow.Cells["Vendor"].Value);
}
}
【问题讨论】:
-
有错误?还是空值?还是什么?
标签: c# datatable datagridview selectionchanged