By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

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

P: 21
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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 750
PRR
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

Post your reply

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