Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:17:05+00:00 2026-06-14T15:17:05+00:00

In my project, I am adding ComboBox, Text, Link label to my DataGridView dgvMain

  • 0

In my project, I am adding ComboBox, Text, Link label to my DataGridView dgvMain.I have created different methods for different cell templates as shown below: (The code below is working)

gridLnklbl(string headerName)

   DataGridViewLinkColumn col = new DataGridViewLinkColumn();
   col.HeaderText = headerName;     //
   col.Name = "col" + headerName;   // same code repeating to all the methods
   dgvMain.Columns.Add(col);        //

gridCmb(string headerName)

   DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
   col.HeaderText = headerName;
   col.Name = "col" + headerName;
   dgvMain.Columns.Add(col);

gridText(string headerName)

   DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
   col.HeaderText = headerName;
   col.Name = "col" + headerName;
   dgvMain.Columns.Add(col);  

As you can see, except the declaration of objects, the code for every method is repeating. Just curious to know, can the repeating code be converted to single method? I dont know how to do that.. Its not about 3 codes of line, I have written many more lines which can be make common to those methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:17:06+00:00Added an answer on June 14, 2026 at 3:17 pm

    I would use a generic Extension Method on DataGridView:

    public static class ExtensionMethods {
    
        public static void AddColumn<TCol>(this DataGridView dgv, string headerName) 
            where TCol : DataGridViewColumn, new()
        {
           var col = new TCol {
               HeaderText = headerName,
               Name = "col" + headerName,
           };
           dgv.Columns.Add(col); 
        }
    }
    

    There is a constraint on TCol that ensures it is a subclass of DataGridViewColumn.

    Invoke the extension method as such:

    dgvMain.AddColumn<DataGridViewLinkColumn>(headerName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a class called connector. By clicking on project and adding a class.
I have created a repository on Github. This repository contains a complete project with
I have a datagrid control in my project which has a combobox control manually
I have connected .wsdl file to my project (by adding service reference ). Wcf
I'm linking a text file into my project by adding it to the resource
I have a combobox named cmbSubjects . It's purpose in my project is to
For my current project I am adding an variable amount of usercontrols : ucTask
I'm adding an android library project to my android app (in this case, Beintoo).
I am adding unit tests to project in Qt and am looking to use
I am adding MVC to a project that has MANY legacy webform pages. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.