简体   繁体   中英

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. I hope can get helps from Stack community. Below is my problem: I have a table in my SQL Server Database

FID| FCode | FName |
_ __ _ __ _ __ _ __ _ __
1 | MAT001 | Material01 |
2 | MAT002 | Material02 |
3 | MAT003 | Material03 |
4 | MAT004 | Material04 |
5 | MAT005 | Material05 |
_ __ _ __ _ __ _ __ _ __

  • And now in the Data Gridview of my application (C#) (My 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.
    I can connect to the data source and change the Gridview column to ComboBox type,then display it to the DataGridView . The ValueMember is FID , the DisplayMember is FCode .
  • The remaining issue is: I'm trying to populate the Item Name (FName) to the second column,when I select an item in the ComboBox , the Item Name beside will update the name automatically. How can I solve this case? Please help!
    Many thanks and warmest regards to our community for helping members.

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.

Within this event handler, you will get a DataColumnChangeEventArgs parameter from which you can check which column is being changed. 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. From this you can determine where to push the Item Name.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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