473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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 2781
PRR
750 Expert 512MB
DataGridView.DataSource 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 IBindingListView 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
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...
2
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...
4
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...
4
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...
1
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...
0
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...
13
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...
9
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.