Hello All,
I need to add one dynamic column to gird view control to the last column(email) , this column not exist in DB.
My existing Gridview code here.Please help me to add i am getting that email column value from Other AD code.
UI
C#
Hello All,
I need to add one dynamic column to gird view control to the last column(email) , this column not exist in DB.
My existing Gridview code here.Please help me to add i am getting that email column value from Other AD code.
UI
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 13, 2023, 7:43 AM EDT
To add a dynamic column to GridView, you can add a new TemplateField to the GridView.Columns collection. You can then set the HeaderText property of the TemplateField to the column header text and add an ItemTemplate to define how the column value will be displayed for each row.
Assuming that you have the email value available in your code-behind, you can add the following code to create the dynamic column:
// Create a new TemplateField TemplateField emailField = new TemplateField(); // Set the header text for the column emailField.HeaderText = "Email"; // Create an ItemTemplate for the column to display the email value emailField.ItemTemplate = new EmailTemplate(); // Add the new column to the GridView GridView1.Columns.Add(emailField);Note that you will need to create a new class for the ItemTemplate. You can do this by creating a new class that implements the ITemplate interface, like this:
public class EmailTemplate : ITemplate { public void InstantiateIn(Control container) { // Create a new Label control to display the email value Label emailLabel = new Label(); // Set the DataBinding event handler for the Label to display the email value emailLabel.DataBinding += new EventHandler(emailLabel_DataBinding); // Add the Label control to the container control container.Controls.Add(emailLabel); } private void emailLabel_DataBinding(object sender, EventArgs e) { // Get the Label control that raised the DataBinding event Label emailLabel = (Label)sender; // Get the current GridView row GridViewRow row = (GridViewRow)emailLabel.NamingContainer; // Get the email value for the current row string emailValue = GetEmailValueForRow(row); // Set the email value as the text of the Label control emailLabel.Text = emailValue; } private string GetEmailValueForRow(GridViewRow row) { // TODO: Replace this with your code to get the email value for the current row return "[email protected]"; } }The GetEmailValueForRow method should be replaced with your code to retrieve the email value for the current row. You can use whatever method you are using to retrieve the email value in your existing code. Once you have added the dynamic column to the GridView, it should display the email value for each row.
Thanks
Naimish Makwana
Amit MohantyRank #112 · 20K reputationPosted Apr 14, 2023, 7:09 AM EDT
Add a TemplateField control to the Columns collection of your GridView control:
Define a function to retrieve the email value for each row:
protected string GetEmailValue(object userId) { // Logic to retrieve email value based on userId string email = ""; return email; }In the GetEmailValue function, you can use the userId parameter to retrieve the email value from your Active Directory code.