简体   繁体   English

如何使用查询C#将文本框值添加到DataGrid单元格

[英]How to add textbox value to datagrid cell using query c#

I'm creating a windows form that will allow my group at work to check in & out computers from our spare cabinet. 我正在创建一个Windows窗体,该窗体将允许我的工作组从备用柜中检入和检出计算机。 I have two tables, an 'Assets' table & Inventory Log table. 我有两个表,“资产”表和“库存日志”表。 The user will fill out the GUI and select the asset they want from the check out screen. 用户将填写GUI并从“签出”屏幕中选择所需的资产。 The problem I am having is trying to pass the foreign key 'InventoryID' to the log table. 我遇到的问题是试图将外键“ InventoryID”传递到日志表。 I have the main textbox's bound to datagrid cells but I am unable to do this with the inventory ID because I have it bound to a textbox so the user does not have to enter it every time. 我将主文本框绑定到了datagrid单元,但是我无法使用清单ID来完成此操作,因为我将其绑定到了一个文本框,因此用户不必每次都输入它。

I have tried creating an insert query that takes the textbox and converts it to int and then inserts it into the method but it catches an exception. 我尝试创建一个插入查询,该查询采用文本框并将其转换为int,然后将其插入到方法中,但它捕获了异常。 Note, this query works when I test it in the query builder. 请注意,当我在查询构建器中对其进行测试时,该查询将起作用。 I have also tried adding it with the binding source property but it cant find the index. 我也尝试过使用绑定源属性添加它,但是找不到索引。

private void BtnSubmit_Click(object sender, EventArgs e)
    {
        //get asset number 
        string asset = txtAssetNumber.Text;
        //get invID from textbox
        int InventoryID = Convert.ToInt32(txtinv.Text);
        //this query changes available status to 'No'              
        inventoryTableAdapter.SetNo(asset);    
        //runs query to determine what rows have 'Yes'
        inventoryTableAdapter.Available(this.loanerCabDataSet.Inventory);

        try
        {        
            //add new row to log table
            inventoryLogBindingSource.AddNew();
            //suppose to insert the ID into table
            inventoryLogTableAdapter.insertinvid(InventoryID);

            //set textbox back to today's date
            txtOutDate.Text = DateTime.Today.ToString("MM/dd/yyyy");

            this.Validate();
            this.inventoryBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.loanerCabDataSet);

            MessageBox.Show("Success");
        }
        catch (Exception)
        {
            throw;
        }

    }

Query: 查询:

INSERT INTO InventoryLog (InventoryID) VALUES (?) 插入到库存日志(InventoryID)值(?)

Finally figured out a way around it. 终于找到了解决之道。

Created insert query and instead of binding all of the textboxes to the column, I created variables and passed them into the function. 创建插入查询,而不是将所有文本框绑定到该列,而是创建变量并将其传递给函数。

Heres what it looks like now. 这是现在的样子。

inventoryLogTableAdapter.Insert(InventoryID, customer, tech, loaneddate, datein, location, ticket, notes); InventoryLogTableAdapter.Insert(InventoryID,客户,技术人员,借出日期,日期,位置,票据,注释);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM