首页 > 解决方案 > 当 DataGridView 中的当前行更改时,绑定的文本框值不会更新

问题描述

我有一个数据集,里面有一个数据表,我有杂项。我的主表单中的文本框绑定到这个数据表。当在也绑定到同一个数据表的 datagridview 中更改选择时,如何更新我的文本框?

可以根据需要向这个问题添加代码。如果有的话,不确定需要什么。

解释的编辑: 下面显示我确实将我的文本绑定到数据集,正如 Caius 在上一个问题中向我展示的那样。

图像显示突出显示的文本框 图像显示相同文本框的属性。

和一些代码显示 datgridview 链接到数据集数据表

        //json file holding all data to be parsed.
        string myDynamicJSON = File.ReadAllText(@"testLibrary.json");

        //the data
        ToolJson ToolData = JsonConvert.DeserializeObject<ToolJson> 
       (myDynamicJSON);

        //DataTable with something in it, do the binding
        BindingSource SBind = new BindingSource();
        SBind.DataSource = tooldataSet.Tables["Tool"];
        


        //looks into File finds json fields, and assign them to 
        variables to be used in C# to create the rows.
        foreach (var datum in ToolData.datum)
        {
            string description = datum.Description;
            string vendor = datum.Vendor;
            double cost = datum.Cost;
            string serial = datum.ProductLink;
            string employee = datum.employee;
            string location = datum.location;
            bool returntool = datum.returnTool;
            int onHand = datum.onHandQty;
            int stockQty = datum.stockQty;
            int orderQty = datum.orderQty;
            string toolType = datum.Type;
            double diameter = datum.Geometry.Dc;
            double OAL = datum.Geometry.Oal;
            string productID = datum.ProductId;


            //Populate the DataTable with rows of data
            DataRow dr = tooldataSet.Tool.NewRow();

            // Fill the values
            dr["Description"] = description;
            dr["Vendor"] = vendor;
            dr["Cost"] = cost;
            dr["Serial #"] = serial;
            dr["Employee"] = employee;
            dr["Location"] = location;
            dr["OnHand"] = onHand;
            dr["StockQty"] =stockQty;
            dr["OrderQty"] = orderQty;
            dr["Return"] = returntool;
            dr["Diameter"] = diameter;
            dr["OAL"] = OAL;
            dr["Type"] = toolType;
            dr["Product Id"] = productID;
            


            //once all data is added to the row, add the row, and loop 
            untill all data is loaded.
            tooldataSet.Tool.Rows.Add(dr);
        }
        //bind our dataset.table to the gridview
        toolDataGridView.DataSource = SBind;

        transactionEmployee_Box.Text = "";
        transactionSerial_Box.Text = "";

标签: c#datagridviewtextbox

解决方案


我找到了自己的答案。我最接近完成这项工作的是, active_Description.text = Convert.ToString(toolDataGridView.CurrentRow.Cells[2]);

但我从来没有意识到我可以添加.value到最后,它工作得很好。 active_Description.text = Convert.ToString(toolDataGridView.CurrentRow.Cells[2].Value)是我需要的。


推荐阅读