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

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;
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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.
Jacques Bourgeois (James Burger)PresidentCommented:
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.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.