Error creating Extension for Datagridview C#?

Asked

Viewed 78 times

1

I’ve used Xtensions for other components and did not give this problem, is giving the error only with the DataGridView. The component finds the extension, and not the error, the error shows only when building the project.

When compiling is giving the following error:

Error 1 'System.Windows.Forms.Datagridview' does not contain a Definition for 'Get Column Names' and no Extension method 'Get Column Names' Accepting a first argument of type 'System.Windows.Forms.Datagridview' could be found (are you Missing a using Directive or an Assembly Reference?) C: Users Nicola Bogar Desktop System Mysolutionapp Windowsformsapplication3 frmCadastroPais.Cs 55 38 Windowsformsapplication3

public static class ExtensionsDataGridView
{
    /// <summary>
    /// Obter os nomes das colunas da DataGridView.
    /// </summary>
    /// <param name="dgv"> Grid.</param>
    /// <returns> Lista com os nomes das colunas. </returns>
    public static List<string> ObterNomeDasColunas(this DataGridView dgv)
    {
        List<string> lista = new List<string>();

        if (dgv == null)
            return null;

        if (dgv.ColumnCount > 0)
            for (int i = 0; i < dgv.ColumnCount; i++)
                lista.Add(dgv.Columns[i].Name);
        return lista;
    }
}

public class Teste
{
   private void frmCadastroPais_Load(object sender, EventArgs e)
   {
      DataGridView dgv = new DataGridView();
      dgv.DataSource = paisBindingSource.DataSource;

      List<string> lista = dgv.ObterNomeDasColunas();
   }
}

1 answer

0


Your mistake in question, is that the namespace corresponding of the extension method or some very particular error, so I will generate a minimal example for you to test and try to solve your problem:

Try to use like this:

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GridExtensions
{
    public static class Utils
    {
        public static IEnumerable<string> GetNamesFromColumns(this DataGridView grid)
        {
            if (grid != null && grid.Columns != null && grid.Columns.Count > 0)
            {
                return (grid.Columns as BaseCollection)
                    .OfType<DataGridViewColumn>()
                    .Select(x => x.Name).ToList();
            }
            return null;
        }
    }
}

Observing: does not often need to generate a for for information extraction where the basis is always collections and the can help assemble a new list.


using GridExtensions; // namespace respectivo do método de extensão...

public class Teste
{
   private void frmCadastroPais_Load(object sender, EventArgs e)
   {
      DataGridView dgv = new DataGridView();
      dgv.DataSource = paisBindingSource.DataSource;

      List<string> lista = dgv.GetNamesFromColumns();
   }
}
  • 1

    Solved friend, strange this right, your code has not changed almost anything of mine, just the way to get the name of the columns. I think it really should be something related to namespace.. Thank you very much anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.