mahesh  kumar

mahesh kumar

  • NA
  • 20
  • 1.1k

dynamically bind item template , edit template to gridview codebehind

Jul 1 2021 5:14 AM

I have multiple tables which need to be bind to the asp.net grdiview at run time during code behind:  one such table example is the employee mapping table which will be bind to grid view.

At the time of editing the grid view, we need to bind the dropdown to some of the columns and these columns display dropdown at the time of editing the data rows. and each dropdown will be sourced for different tables and different select and display fields. 

for the city, it has been multi-select dropdown values when the grid is in edit mode. can this be done?

employee_mapping
  id  employee id   qualificationid cityid
100  232  161   86,87
101 233   162    85,84
qualification
 qualificationid qualificationname
 161   BE (IT)
162   Btech (CSE)
163 MSCIT
164 MBBS

  

city table
city id city name
86    Rajkoat
87 ahmedabad
88

   pune

Employee table
employeeid  emplyeename
 232  Nilesh
233  purnima

once the gridview is loaded, it should display the employee_mapping table by joining city and qualification tables to show cityname and qualificationname instead of their ids in gridview. 

once grdiview is in edit mode , dropdown should be visible:   in dropdown,  data display field should be "qualification Name" and data selected value field must be "qualification id"  from qualification table.

can we do this at run time using code behind .?

for simple bound fields, we can do simply as : 

BoundField nameColumn = new BoundField();
nameColumn.DataField = "mappingID";
nameColumn.HeaderText = "Employee ID"; 
GridView1.Columns.Add(nameColumn);

but how to bind the template fields and item template and edit item templates along with multi select dropdowns and saving the values to database?


Answers (1)