Results 1 to 8 of 8

Thread: How to generalize the name of a DataGridView object?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Question How to generalize the name of a DataGridView object?

    Hi,
    I want to make a dll library for exporting DataGridView content to Excel file. The code works pretty fine in the main windows form. But I want to generalize it to create dll file for using in other projects. My problem is that I cannot generalize DataGrideView name in my class code.
    For example, a part of my code is:
    Code:
    for (int i = 1; i < DataSource.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = DataSource.Columns[i - 1].HeaderText;
                }
    DataSource has not defined. It has planned to be the name of a potential DataGrideView. How should I define DataSource ? (My code is in C#)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to generalize the name of a DataGridView object?

    You need to define a class in this library that contains a method that does this work on the grid. You declare a parameter on that method of type DataGridView and then use that in the code. Any application that contains a DataGridView can then call that method and pass the grid as an argument.
    vb.net Code:
    1. Public Class DataGridViewProcessor
    2.  
    3.     Public Sub ExportToExcel(grid As DataGridView)
    4.         'Use grid here.
    5.     End Sub
    6.  
    7. End Class
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     Dim processor As New DataGridViewProcessor
    3.  
    4.     processor.ExportToExcel(DataGridView1)
    5. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Question Re: How to generalize the name of a DataGridView object?

    Quote Originally Posted by jmcilhinney View Post
    You need to define a class in this library that contains a method that does this work on the grid. You declare a parameter on that method of type DataGridView and then use that in the code. Any application that contains a DataGridView can then call that method and pass the grid as an argument.
    vb.net Code:
    1. Public Class DataGridViewProcessor
    2.  
    3.     Public Sub ExportToExcel(grid As DataGridView)
    4.         'Use grid here.
    5.     End Sub
    6.  
    7. End Class
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     Dim processor As New DataGridViewProcessor
    3.  
    4.     processor.ExportToExcel(DataGridView1)
    5. End Sub
    DataGridView cannot be defined in C#.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: How to generalize the name of a DataGridView object?

    Yes it can. It's a type in the framework, and that is language independent. It sounds like the library is based on .NET Core, not .NET Framework. As far as I can tell, .NET Core does not have a DataGridView.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: How to generalize the name of a DataGridView object?

    Quote Originally Posted by Shaggy Hiker View Post
    Yes it can. It's a type in the framework, and that is language independent. It sounds like the library is based on .NET Core, not .NET Framework. As far as I can tell, .NET Core does not have a DataGridView.
    Does it mean that C# is weaker language in comparison with VB.NET?
    Last edited by fa2020; Nov 17th, 2020 at 02:22 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to generalize the name of a DataGridView object?

    Quote Originally Posted by fa2020 View Post
    Is it mean that C# is weaker language in comparison with VB.NET?
    No, it doesn't mean that at all. I didn't even register the C# bit at the start. Why the hell have you posted in the VB.NET forum in the first place if this is a C# question? How about you respect us and the forum enough to post C# questions in the C# forum?

    Anyway, the issue likely has nothing at all to do with C# and you just don't know the fundamentals of assemblies and references. If you create a Windows Forms Application project then references to the System.Windows.Forms.dll assembly and a few others are added automatically. That's where the declarations of all the WinForms controls, including the DataGridView class, are made. If you use the Class Library project template then some of those references, including System.Windows.Forms.dll are not added, because most libraries won't need them. If you do need one or more of those references then you have to add them yourself, just as you do any other reference. This applies to C# and VB.NET.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to generalize the name of a DataGridView object?

    Quote Originally Posted by Shaggy Hiker View Post
    Yes it can. It's a type in the framework, and that is language independent. It sounds like the library is based on .NET Core, not .NET Framework. As far as I can tell, .NET Core does not have a DataGridView.
    WinForms has been supported in .NET Core since version 3.0. That said, a WinForms application is likely targeting .NET Framework so any library to support such an application should target .NET Framework too.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: How to generalize the name of a DataGridView object?

    Thread moved from the 'VB.Net' forum to the 'C#' forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width