【问题标题】:i need a way to show records on a new form by clicking on a datagridview on another form我需要一种通过单击另一个表单上的 datagridview 在新表单上显示记录的方法
【发布时间】:2020-01-13 00:12:13
【问题描述】:

我有一个带有显示库存的 datagridview 的表单。库存表有一个名为 unitid 的字段。

我希望它能够工作,这样当我从库存数据网格视图中选择一条记录时,它必须从例如 frminventorydetails 打开另一个记录,并从我选择的记录中显示文本框和组合框中的所有字段。

我有它的工作,但问题是组合框。它没有在组合框中显示正确的值。当详细信息表单加载时,我有一种方法可以使用单位表中的值加载组合框。但是当详细信息表单加载时,它不会转到相应的显示成员。

当我将组合框以与库存数据网格相同的形式放置时,它可以完美运行。但当组合框是另一种形式时不起作用。

  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);
    }

我希望组合框为我选择的特定项目显示适当的单位描述,但它只保留在第一条记录上。

如果组合框与数据网格位于同一表单上,则它可以工作,但如果我打开一个新表单,则不会。

【问题讨论】:

  • 我认为 invdetopen.cbunits 没有正确初始化。在显示对话框之前检查存储的 invdetopen.cbunits.DataSource。

标签: mysql datagridview combobox openform


【解决方案1】:

您没有显示用于设置cbunitsValueMemberDisplayMember 的代码。确保设置了这些属性,然后设置了ComboBoxDataSource最后您可以使用SelectedValue

另外,这可能是个问题:

string units = "select * from tblunits";

必须匹配属性(显示和值)名称,因此我强烈建议在 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”上方的小放大镜),您实际上会看到它是一个表格。

如果操作正确,不会超过上面那几行代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多