我有一个带有datagridview的表格,显示了库存。库存表中有一个名为unitid的字段。
我希望它能正常工作,以便当我从库存数据网格视图中选择一条记录时,它必须从frminventorydetails中打开另一个记录,并显示所选记录中文本框和组合框中的所有字段。
我可以使用它,但是问题是组合框。它不会在组合框中显示正确的值。当详细信息表单加载时,我有一种方法可以用units表中的值加载组合框。但是在加载详细信息表单时,它不会转到相应的显示成员。
当我以与库存数据网格相同的形式放置组合框时,它可以完美工作。但是在组合框为其他形式时不起作用。
private void dginventory_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
string getdetails = @"select * from tblinventory where
tblinventory.invid='" + Convert.ToInt32(dginventory.CurrentRow.Cells[0].Value.ToString()) + "'";
performqueries.singleResult(getdetails);
frmviewinvdetails invdetopen = new frmviewinvdetails();
if (performqueries.dt.Rows.Count > 0)
{
invdetopen.txtinvid.Text = performqueries.dt.Rows[0].Field<int>("invid").ToString();
invdetopen.txtinvcode.Text = performqueries.dt.Rows[0].Field<string>("InventoryCode").ToString();
invdetopen.txtInvDescription.Text = performqueries.dt.Rows[0].Field<string>("InvDescription").ToString();
invdetopen.txtInvShortText.Text = performqueries.dt.Rows[0].Field<string>("InvShortText").ToString();
//here im assigning the unitid to the selectedvalue property of the combobox in the details form.
invdetopen.cbunits.SelectedValue = Convert.ToInt32(performqueries.dt.Rows[0].Field<int>("unitid").ToString());
}
invdetopen.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
// this fills the combobox on the details form when it loads
private void frmviewinvdetails_Load(object sender, EventArgs e)
{
SqlConfig performcrud = new SqlConfig();
string units = "select * from tblunits";
performcrud.fill_CBO(units, cbunits);
}
我希望组合框为我选择的特定项目显示适当的单位描述,但它只会保留在第一条记录中。
如果组合框与数据网格具有相同的格式,则有效,但如果我打开新窗体,则无效。
答案 0 :(得分:0)
您不会显示用于设置ValueMember
中的DisplayMember
和cbunits
的代码。确保设置了这些属性,然后设置了DataSource
中的ComboBox
,并且最终您可以使用SelectedValue
。
另外,这可能是个问题:
string units = "select * from tblunits";
您必须匹配属性名称(Diplay和Value),所以我强烈建议在SQL中使用特定的字段名称。我不知道它是否还可以使用'*'。 编辑:我测试了它,它与asterix一起使用,因此您只需要确保字段名称匹配即可。。另外,将DataTable
用作DataSource
将是一个优势:
dt Datatable = FunctionToGetDataTable("select ID, UnitName as UnitName from tblunits");
因此,您可以使用:
invdetopen.cbunits.ValueMember = "ID";
invdetopen.cbunits.ValueMember = "UnitName";
invdetopen.cbunits.ValueMember = dt;
您应该始终能够使用断点停止代码并查看变量。如果您使用DataTable
,则可以在调试模式下利用DataTable Visualizer(在“ dt”上单击小放大镜),然后将其视为表格。
只要操作正确,它所花的时间不会超过上面几行。