dataGridView1.DataBind(); is for web, modify for windows app

Posted on 2014-10-02
Last Modified: 2014-10-05
I am following a training video prepared for web apps:
        private void frmCS01_Load(object sender, EventArgs e)
            // MessageBox.Show(string.Format("msg form{0}", !string.IsNullOrEmpty(cnString) ? string.Format(" - {0}", cnString) : "")); 
            SqlConnection cnn = new SqlConnection(cnString);
            SqlCommand cmd=new SqlCommand("Select * From tblProduct", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            dataGridView1.DataSource = dr;

Open in new window

Using it for a window app, there is error at line 10 (dataGridView1.DataBind();)

Q1: How can I correct that (error: 'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind'; so what is its replacement?

Q2: Is it possible to have different forms in different languages in a solution? (like Form1 in and Form2 in c#; I think the answer is no but just want to check with an expert if it is possible).

Q3: Is it possible in windows forms and webforms in the same solution and the same language? ( or c#)

Just in case you are wondering why these question, I want to have fewer codeSample files.

Revised: I got this far but not working yet. perhaps no records is coming from the database. How can I check if it does or not:

            // MessageBox.Show(string.Format("msg form{0}", !string.IsNullOrEmpty(cnString) ? string.Format(" - {0}", cnString) : ""));
            SqlConnection cnn = new SqlConnection(cnString);
            SqlCommand cmd=new SqlCommand("Select * From tblProduct", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
           // SqlDataAdapter da= new SqlDataAdapter();
            DataTable dt = new DataTable();
            dataGridView1.DataSource = dt;
Question by:Mike Eghtebas
  • 3
  • 2
LVL 40
ID: 40358401
The GridView and the DataGridView are 2 different controls with a few similarities, but a lot of differences. Any code that you see that works with one does not work with the other. For a DataGridView, the assignment to DataSource is sufficient, the databinding is done for you in the background.

Your main problem in your last piece of code is that you assign the DataTable to the DataSource, and the DataTable is empty.

With the DataAdapter, you would need to call its Fill method to fill the DataTable.

ExecuteReader connects the DataReader with the database, but does not read the values. Usually, you need to loop with its Read method to retrieve the data.

But the DataReader has been designed so that if you assign it to a DataSource, it will automatically perform the loop for your.

Try dataGridView1.DataSource = dr  // instead of dt

If you do not have a result, check the value of the HasRows property of the DataReader after the call to ExecuteReader. If it is false, then nothing was returned from the database.

You can have different forms in different languages, but in a solution, not in a project. A solution is a group of projects (often a .exe and a few .dll), and each one can be in a different language. But a specific project can have code in only one language.

Same thing for web vs Windows application. In the same solution yes, but in the same project no.

Same thing for
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40358430
This is a good news. Lets say there is a sample1 in, I can have one solution that contains the following 4 projects:


If so, I owe you a lunch.

---- on the gridview vs datagridview I found this:
A DataGridView is a WinForms grid used to display rows of data with multiple columns.  A GridView is the ASP.NET equivalent.  They both perform similar functions although the underlying details are quite a bit different.  Prior to v2 the DataGrid was used for both WinForms and ASP.NET apps.  They were two separate controls with the same name.  Since they behaved so differently it caused a lot of confusion when someone would ask a question about the DataGrid.  Hence in v2 the controls have different names.

Although the controls look and act similar they are quite a bit different.  The DataGridView contains columns deriving from DataGridViewColumn whereas a GridView has columns deriving from DataControlField.  Perhaps a bad name but here to stay nonetheless.  Rows in a GridView derive from GridViewRow wherease DataGridView rows deriving from DataGridViewRow.  Only MS knows why DataControlField was used instead of GridViewColumn.  There are quite a few other differences as well.  When asking questions about either of these controls make it clear which one you are using.
LVL 40
ID: 40358548
You owe me a lunch. I just hope that you live in San Francisco and also pay the trip from Montreal.

Yes you can.

If the projects already exist, simply open your VB project. In the File menu, go to Add...Existing Project and repeat for the 3 other projects.

In other situations, if you have one of the projects started and want to add a second new project to the solution, go File again, New Project, select the type of project, and make sure that the Solution dropdown is set to Add to Solution.

Note that this does not mean that you can readily call the code in one project from another one. Even if they are in the same solution, they are individual projects. the WinVB project needs to reference the WinCS project in order to use its classes for instance. And usually, you need to design the WinCS so that you are expecting it to be called from another application. That is one of the reasons behind dlls.

The Asp projects being built around HTML pages, do not think that you will display these pages easily in the WinVB Windows Forms and vice-versa.

And just because you can do it does not mean that you should. I have a few solutions that have a mix of VB in one project and C# in the other one. Having to switch back and forth in the same programming session is not easy even for a programmer who is experienced in both. Even if the framework is the framework and you end up doing the same thing with both, you tend to think differently when you are in C# and when you are in VB. It's even worse between a Windows Application, a Class Library and an ASP.NET application.

I also have a few solutions that have different types of projects in them, such as a Windows application in one project and a service in the other. I always try to concentrate my work in a session on one or the other, not on both at the same time. The way I think while I am working in a service that might be used by different applications is a lot different than the way I think when working in an application where the interface and the way you handle events is often more important than the code.

As I told you, The GridView and the DataGridView are 2 different controls with a few similarities, but a lot of differences. Do not try to grasp then both at the same time, otherwise you will get confused. Do not try either to grasp VB and C# at the same time. Do not try to learn Web development at the same time you are learning Windows development. These things need to be learned one at a time.
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40360771
Hi James,

re:> Try dataGridView1.DataSource = dr  // instead of dt

This doesn't work. I checked dr.HasRows (it has rows).

I do not know what I should be doing now. Are you sure I do not need a data table first?


revised:  I got it now. this one works:
Dim source As New BindingSource
  source.DataSource = datareader
  datagrid1.DataSource = source
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 40360828
You're right. I have not done that in a while, because nowadays I use DataReaders mostly to fill in my own collections, that are then assigned to the DataGridView.DataSource. In my memory, you could assign a DataReader directly to the DataGridView, but its not the case. It's clear when you look at the documentation for the DataReader, it does not implement the necessary interface to be able to be used with a standard DataSource. The role of the BindingSource is to provide what is needed.

Sorry for the confusion I might have caused. I am glad to see that you found the missing piece in the puzzle.

If the data in the grid does not need to be posted back to the database after modifications, you are better with the DataReader approach. Because it is readonly, the DataReader requires less resources than a DataTable.

If however you let the user edit the data and want to send the changes back to the database, then a DataTable might be more appropriate, because it can be used to easyly (most of the time) update the database through a DataAdapter.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now