[英]Update TextBox column in DataGridView when change DataGridView ComboBox value
I have some problem when trying to work with Data Gridview in C# Windows Forms programming. 尝试在C#Windows Forms编程中使用Data Gridview时遇到一些问题。 I hope can get helps from Stack community. 我希望可以从Stack社区获得帮助。 Below is my problem: I have a table in my SQL Server Database 下面是我的问题:我的SQL Server数据库中有一个表
FID| FID | FCode | FCode | FName | FName |
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __
1 | 1 | MAT001 | MAT001 | Material01 | 材质01 |
2 | 2 | MAT002 | MAT002 | Material02 | 材质02 |
3 | 3 | MAT003 | MAT003 | Material03 | 材质03 |
4 | 4 | MAT004 | MAT004 | Material04 | 材质04 |
5 | 5 | MAT005 | MAT005 | Material05 | 材质05 |
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __
DataGridView
has 2 columns, 1 is the Item Code, 1 is Item Name) , the Item Code column is a ComboBox
control which will display the FCode value. 现在在我的应用程序(C#)的Data Gridview中(我的DataGridView
有2列,其中1是项目代码,1是项目名称),该项目代码列是ComboBox
控件,它将显示FCode值。 ComboBox
type,then display it to the DataGridView
. 我可以连接到数据源并将Gridview列更改为ComboBox
类型,然后将其显示为DataGridView
。 The ValueMember is FID , the DisplayMember is FCode . ValueMember是FID, DisplayMember是FCode 。 ComboBox
, the Item Name beside will update the name automatically. 剩下的问题是:我试图在第二栏中填充项目名称(FName),当我在ComboBox
选择一个项目时,旁边的项目名称将自动更新名称。 How can I solve this case? 如何解决这种情况? Please help! 请帮忙! One quick and dirty way of achieving this would be to handle the DataTable.ColumnChanging
event for the table to which your DataGridView is bound. 一种快速而又肮脏的方法是处理与您的DataGridView绑定的表的DataTable.ColumnChanging
事件。
Within this event handler, you will get a DataColumnChangeEventArgs
parameter from which you can check which column is being changed. 在此事件处理程序中,您将获得一个DataColumnChangeEventArgs
参数,从中可以检查要更改的列。 You can simply check if it's your Item Code column changing, and if so, lookup the value to be pushed to the corresponding Item Name column. 您可以简单地检查它是否在更改您的“项目代码”列,如果是,则查找要推送到相应“项目名称”列的值。
The DataColumnChangeEventArgs
gives you the Column and also the Row that are being changed. DataColumnChangeEventArgs
为您提供要更改的列和行。 From this you can determine where to push the Item Name. 由此您可以确定将项目名称推送到何处。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.