【发布时间】:2021-11-02 13:20:41
【问题描述】:
我有一个数据集,里面有一个数据表,我还有杂项。我的主表单中的文本框绑定到这个数据表。如何在也绑定到同一个数据表的 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 = "";
【问题讨论】:
-
您能否更新您的帖子以包含Minimal, Reproducible Example,以便我们进一步帮助您?
-
确定一下
-
文本框和dgv真的是通过同一个绑定源绑定的吗?如果没有,添加绑定源,将其绑定到表格,将控件绑定到 BS,然后填充表格.. 一切都会工作..
-
我用
active_Description.text = Convert.ToString(toolDataGridView.CurrentRow.Cells[2].Value)弄明白了 -
从发布的代码中我不太清楚数据是如何进入表格的。我认为这不是 Newtonsoft 做的吗?此外,您还展示了将文本框绑定到 toolBindingSource 的屏幕截图,但您没有在代码中使用该 BS 来附加到工具数据..?这里似乎存在中断/未连接问题
标签: c# datagridview textbox