473,833 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to show data in the data gridview by using data source.

21 New Member
Hi everybody...

I want to show data in the data gridview placed in my form after getting data from my data access layer class through a class...

Here is my code in the data access layer class.
Expand|Select|Wrap|Line Numbers
  1. public class DBHandler
  2.     {
  3.         public List<Programs> GetAllPrograms()
  4.         {
  5.  
  6.             SqlConnection con = new SqlConnection("Data source=ALIEN/SQLEXPRESS;Database=FinalProjectDB;Integrated security=True");
  7.             SqlCommand com = new SqlCommand();
  8.             com.CommandType = System.Data.CommandType.Text;
  9.             com.CommandText = "SELECT * FROM Programs";
  10.             com.Connection = con;
  11.  
  12.             List<Programs> Program = new List<Programs>();
  13.  
  14.             SqlDataReader reader;
  15.  
  16.             con.Open();
  17.  
  18.             reader = com.ExecuteReader();
  19.  
  20.             if (reader.HasRows)
  21.             {
  22.                 while (reader.Read())
  23.                 {
  24.                     Programs p = new Programs();
  25.                     p.Id = Convert.ToInt32(reader["ProgramID"]);
  26.                     p.Title1 = reader["Title"].ToString();
  27.  
  28.                     Program.Add(p);
  29.                 }
  30.  
  31.             }
  32.  
  33.             con.Close();
  34.  
  35.             return Program;
  36.  }
  37.  
  38.  
  39.       }
  40.  
  41. }  
  42.  

And the code in my form is.

Expand|Select|Wrap|Line Numbers
  1.  public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.     private void Form1_Load(object sender, EventArgs e)
  8.         {
  9.  
  10.             DBHandler ObjDBHandler = new DBHandler();
  11.  
  12.        dataGridView3.DataSource = ObjDBHandler.GetAllPrograms();
  13.        //I need code here to display Id and Name.   
  14.  
  15.  }
  16.  
  17. }
  18.  
The confusion is in the portion of the program which i bold. here is a function naming GetAllPrograms getting values from the above mentioned class what i want is to show the Id and the Name in the gridview.
Does any one help me to sort out the problem...
Sep 7 '09 #1
1 2831
PRR
750 Recognized Expert Contributor
DataGridView.Da taSource property supports

The IList interface, including one-dimensional arrays.

The IListSource interface, such as the DataTable and DataSet classes.

The IBindingList interface, such as the BindingList<(Of <(T>)>) class.

The IBindingListVie w interface, such as the BindingSource class.

You will have to implement Ilist in your "Program" class.
Sep 7 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5679
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the GridView to allow entry of quantity and Y/N switch for
2
6555
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row *that is going to be deleted* into another table, before the original data is deleted. I am trying to use the RowDeleting method to call an Update or Insert
4
2621
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I re-format the updated row fields. I have looked at gridView.rowUpdated method, but cannot figure out how....
4
7607
by: Congero | last post by:
I'm trying to find a way to bind hierarchical data to a gridview control. I've been able to do this with some third party controls and was wondering if this functionality is available with the gridview control. Does anyone have a guidance on this? Thanks -- Live long, stay strong
1
2315
by: Siva | last post by:
Hi, I have a 3 tier ASP.Net app for a handheld which needs to fetch orders from database via a DAL and populate it in a gridview using objectdatasource. In the search page I have a few parameters and when the search button is clicked I am sending those parameters via query string ( I need to do this since my app will run in a handheld which has certain restrictions). In the order list page, I am passing those parameters to the DAL as...
0
1608
by: H5N1 | last post by:
Hi there It seems I've spotted interesting problem in a job I've got to do. I have 3 tables: meals, employees, and intersecting table: meals_for_employees with bool field meaning, that given employee wants a given meal. employees -----------------
13
10794
by: Tomasz Jastrzebski | last post by:
Helo All, The problem: GridView control does not render at all (header/footer) when the data source is empty. I have seen a similar question posted already, but I just can not believe there is no simple solution. I do not believe ASP.Net team took this, sort of, innovative approach and left no way out. Could anybody advise?
9
2746
by: SAL | last post by:
Hello, I have a Dataset that I have table adapters in I designed using the designer (DataLayer). I have a business logic layer that immulates the DataLayer which may/may not have additional logic in. My business classes are, of course, decorated with the: <System.ComponentModel.DataObject() attribute. So, I drop a GridView on a webform and set its datasource to an ObjectDatasource which in turn is using one of my business logic...
0
1309
by: jaredciagar | last post by:
Hi Guys, Can You Help Me PLease, I'm Currently facing Problems in my system... I need some help... I'm using VB script,ASP.net,MS SQL Server2005 I want to select a specific data in GridviewSummary, the specific data that I selected it will display the its details in GridviewDetails: I have a 2 gridview the gridviewsummary and gridviewdetails: gridviewsummary-contains Itemcode field(Primary key)
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10543
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10213
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.