Solved

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

Posted on 2014-10-02
5
348 Views
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(cnString);
            // 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);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dataGridView1.DataSource = dr;
            dataGridView1.DataBind();
        }

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 vb.net 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? (vb.net 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(cnString);
            // 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);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
           // SqlDataAdapter da= new SqlDataAdapter();
            DataTable dt = new DataTable();
            dt.Load(dr);          
            dataGridView1.DataSource = dt;
            //dataGridView1.DataBind();
0
Comment
Question by:Mike Eghtebas
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
0
 
LVL 33

Author Comment

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

sample01WinVB
sample01WinCS
sample01AspVB
sample01AspCS

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.
Mike
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
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?

Mike

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

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

12 Experts available now in Live!

Get 1:1 Help Now