How to display records from a database without using DataGridView

Lin100

Well-known member
Joined
Dec 12, 2022
Messages
69
Programming Experience
10+
In Ms Access database, a form can display records from a table or query
in a single record as oppose to multiple records like a DataGridView

I want to display records from an Access database on a C# form without using DataGridView
because there are too many fields. When using DataGridView, you have to scroll left and right if
there are too many fields.

The attachment depict a form that has many fields but with just one record. To see the second
record, or third or fourth, the user just click on the arrow below. I want a C# form to do the same.
How would I do this in C# ?
 

Attachments

  • Reservation Form That Display One Record At A Time.jpg
    Reservation Form That Display One Record At A Time.jpg
    173.2 KB · Views: 75
Query the database and populate a list of your desired type with the data, e.g. populate a DataTable using a data adapter or data reader. Bind the DataTable to a BindingSource and then bind that to your controls, e.g.
C#:
myBindingSource.DataSource = myDataTable;
givenNameTextBox.DataBindings.Add("Text", myBindingSource, "GivenName");
familyNameTextBox.DataBindings.Add("Text", myBindingSource, "FamilyName");
You can then navigate through the data by calling MoveNext and MovePrevious on the BindingSource. You can also associate the BindingSource with a BindingNasvigator to provide a complete navigation UI.
 
I think what the OP is asking for is some magic C# that will generate a UI form for him that has records navigator built into the form -- pretty much like what MS Access provides for navigating through a table.
 
I think what the OP is asking for is some magic C# that will generate a UI form for him that has records navigator built into the form -- pretty much like what MS Access provides for navigating through a table.
If that is the case then it actually does exist. If you use the Data Sources window to generate a typed DataSet then that window will display all the tables and you can drag them to the form to have the UI generated for you. By default, dragging and dropping a table will generate a DataSet, table adapter, DataGridView, BindingSource and BindingNavigator in the designer and a bit of code to populate the appropriate DataTable when the form loads. You can change that table from grid view to details view in the Data Sources window first and then drag it and, instead of a DataGridView, a discrete control will be generated for each column, along with the appropriate data-bindings. Some manual intervention may be required, e.g. if you need a ComboBox rather than a TextBox, but the majority of the work is done for you.

Most experienced developers don't do that because they separate the data access code form the presentation code but, if you're going to mix them anyway, it's no worse to have the system do much of the work for you.
 
Ah, but that's not C# magic. That's Visual Studio magic. If the user were doing everything in notepad or some other plain text editor, how would they get the strongly typed data sets and corresponding UI?
 
The code below did not put the data into the field Reservation_Number.
The attachment has a JPEG file which depict nothing is in the Reservation_Number
field.

Reservation_Number.DataBindings.Add("Text", myBindingSource,
"Reservation_Number"); Line 54

Reservation3.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
   public partial class Reservation3 : Form
   {
      public Reservation3()
      {
         InitializeComponent();
      }

   private void Reservation3_Load(object sender, EventArgs e)
   {
      this.reservationTableAdapter1.Fill(this.aMS_2007DataSet.Reservation);
      Display_Data_On_Text_Box();
   }

   private void Display_Data_On_Text_Box()
   {
      OleDbConnection con = new OleDbConnection();
      con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
      con.Open();

      OleDbCommand cmd = new OleDbCommand();

      if (comboBox_Name_3.Text == "Select All Properties")
         cmd.CommandText = "SELECT Reservation.* FROM Reservation " +
                           "ORDER BY Reservation.Property_Name, Reservation.Date_Reserved DESC";
      else
        {
           MessageBox.Show("else condition");
           cmd.CommandText = "SELECT Reservation.* FROM Reservation " +
                             "WHERE Reservation.Property_Name = '" + comboBox_Name_3.Text + "'" +
                             "ORDER BY Reservation.Property_Name, Reservation.Date_Reserved DESC";
        }

      cmd.Connection = con;
      OleDbDataAdapter Data_Adapter = new OleDbDataAdapter(cmd);
      DataSet Data_Set = new DataSet();
      Data_Adapter.Fill(Data_Set);

      BindingSource myBindingSource = new BindingSource();
      myBindingSource.DataSource = Data_Set;
      Reservation_Number.DataBindings.Add("Text", myBindingSource, "Reservation_Number");
    }
 }
 

Attachments

  • Reservation3.jpg
    Reservation3.jpg
    33.3 KB · Views: 58
I'm currently not in a position to do any testing, but try using this instead:
C#:
Reservation_Number.DataBindings.Add("Text", myBindingSource, "Reservation.Reservation_Number");
 
Firstly, don't add the BindingSource in code. Add it in the designer.

As for the issue, I posted several times to bind the DataTable to the BindingSource but you have bound a DataSet. If you do what I said then it would work. If you assign a DataSet to the DataSource of the BindingSource then you should first set the DataMember to the name of the DataTable.
 
STOP! The code you provided in post #6 shows that you are using a typed DataSet but then not using it. Make up your mind. If you have a typed DataSet then use it. Don't create data adapters. Don't create a DataSet. Create an instance of your typed DataSet and use the appropriate table adapters to retrieve and save data. Either use a typed DataSet or don't. Do not mix and match.
 
Firstly, don't add the BindingSource in code. Add it in the designer.

As for the issue, I posted several times to bind the DataTable to the BindingSource but you have bound a DataSet. If you do what I said then it would work. If you assign a DataSet to the DataSource of the BindingSource then you should first set the DataMember to the name of the DataTable.

I created a DataTable. I ran and there is no error, but the field Reservation_Number is still blank. If there is any problem with the code then please correct it with specific code.

Revised Code Using DataSet:
   private void Display_Data_On_Text_Box()
   {
      OleDbConnection con = new OleDbConnection();
      con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
      con.Open();

      OleDbCommand cmd = new OleDbCommand();

      if (comboBox_Name_3.Text == "Select All Properties")
         cmd.CommandText = "SELECT Reservation.* FROM Reservation " +
                           "ORDER BY Reservation.Property_Name, Reservation.Date_Reserved DESC";
      else
        {
           MessageBox.Show("else condition");
           cmd.CommandText = "SELECT Reservation.* FROM Reservation " +
                             "WHERE Reservation.Property_Name = '" + comboBox_Name_3.Text + "'" +
                             "ORDER BY Reservation.Property_Name, Reservation.Date_Reserved DESC";
        }

      cmd.Connection = con;
      OleDbDataAdapter Data_Adapter = new OleDbDataAdapter(cmd);
      DataTable Data_Table = new DataTable();
      Data_Adapter.Fill(Data_Table);
      BindingSource myBindingSource = new BindingSource();
      myBindingSource.DataSource = Data_Table;
      Reservation_Number.DataBindings.Add("Text", myBindingSource, "Reservation.Reservation_Number");
    }
 
Argh! When you call Add, the last argument is the column name. Skydiver told you to add the table name to qualify that because you were assigning the DataSet as the data source. Now that you're doing what I told you to do in the first place and assigning a DataTable as the data source, you should also do what I told you and specify the column name unqualified.
 
For future reference, when someone directs you to a type or member that you haven't used before, the first thing you should do is read the documentation for that type or member. Given that you can just click it and press F1 to do that, there's no good reason to not do so. That way, you can get an understanding of how it works and then you will understand what you've been told to do and why and hopefully be able to fix problems like this yourself, instead of throwing stuff at the wall and having no idea why it sticks or doesn't. ALWAYS do what you can for yourself first and you can ALWAYS read the relevant documentation. If there's something you specifically don't understand, ask about that specifically to get that understanding, rather than just posting code you don't understand and asking for another solution that you don't understand. Do what you can to understand.
 
Also bear in mind that if you've created a WinForms NET Core (Net5/6/7) project ratehr tahn Framework, then getting data binding to work in the designer is much more of a pain in the ass. It's full of bugs so MS just turned it off. It's easier to just use a .NET Framework project

Did you ever post the code I asked for? I was planning on using it to show you how to make your life easy - as it stands here, you're still pretty much buying a dog and barking yourself
 
Also bear in mind that if you've created a WinForms NET Core (Net5/6/7) project ratehr tahn Framework, then getting data binding to work in the designer is much more of a pain in the ass. It's full of bugs so MS just turned it off. It's easier to just use a .NET Framework project

Did you ever post the code I asked for? I was planning on using it to show you how to make your life easy - as it stands here, you're still pretty much buying a dog and barking yourself

To cjard:

Are you referring to the post "Procedure did not return an expected value" with link below?

In this post you wanted me to put all files (except bin and obj) into a zip files. and post it ?
I will do this later, but since I do not have Zip application installed in my computer,
I will attach the individual files (unit_2.cs, unit_2.designer.cs, Reservation_2.cs,
Reservation_2.designer.cs, program.cs, app.config)
 
You don't need .ZIP application on modern Windows. You can select multiple files/folders in the File Explorer, then Right click, and use Send to...Compressed (zipped) folder:
1672862851823.png
 

Latest posts

Back
Top Bottom