Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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(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();
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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
Avatar of Mike Eghtebas

ASKER

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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial