DataReader Binding : DataBinding DataGrid « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » DataBinding DataGrid 
23.78.4.DataReader Binding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;

   class Form1 : Form
   {
      private void OnFormLoad(object sender, EventArgs e)
      {
         SqlConnection conn = new SqlConnection("server=localhost;database=Northwind;trusted_connection=true");
         SqlCommand cmd = new SqlCommand("SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers", conn);
         using (conn)
         {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            m_CustomersBindingSource.DataSource = reader;
         }
      }

      private void OnGetItem(object sender, EventArgs e)
      {
         int pos;
         bool success = int.TryParse(m_ItemTextBox.Text,out pos);
         if (!success)
         {
            pos = m_CustomersBindingSource.Position;
         }
         DbDataRecord row = m_CustomersBindingSource.List[posas DbDataRecord;
         MessageBox.Show("Customer: " + row["CompanyName"]);

      }

      public Form1()
      {
         InitializeComponent();
         m_Grid.AutoGenerateColumns = true;
      }

      private void InitializeComponent()
      {
         this.m_ItemTextBox = new System.Windows.Forms.TextBox();
         this.m_Grid = new System.Windows.Forms.DataGridView();
         this.m_GetItemButton = new System.Windows.Forms.Button();
         this.m_CustomersBindingSource = new System.Windows.Forms.BindingSource();
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).BeginInit();
         ((System.ComponentModel.ISupportInitialize)(this.m_CustomersBindingSource)).BeginInit();
         this.SuspendLayout();
         // 
         this.m_ItemTextBox.Location = new System.Drawing.Point(94197);
         this.m_ItemTextBox.Name = "m_ItemTextBox";
         this.m_ItemTextBox.Size = new System.Drawing.Size(10020);
         this.m_ItemTextBox.TabIndex = 5;
         this.m_ItemTextBox.Text = "0";
         // 
         this.m_Grid.AutoGenerateColumns = false;
         this.m_Grid.DataSource = this.m_CustomersBindingSource;
         this.m_Grid.Location = new System.Drawing.Point(1212);
         this.m_Grid.Name = "m_Grid";
         this.m_Grid.Size = new System.Drawing.Size(465176);
         this.m_Grid.TabIndex = 4;
         // 
         this.m_GetItemButton.Location = new System.Drawing.Point(12195);
         this.m_GetItemButton.Name = "m_GetItemButton";
         this.m_GetItemButton.Size = new System.Drawing.Size(7523);
         this.m_GetItemButton.TabIndex = 3;
         this.m_GetItemButton.Text = "Get Item:";
         this.m_GetItemButton.Click += new System.EventHandler(this.OnGetItem);
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(503232);
         this.Controls.Add(this.m_ItemTextBox);
         this.Controls.Add(this.m_Grid);
         this.Controls.Add(this.m_GetItemButton);
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.OnFormLoad);
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).EndInit();
         ((System.ComponentModel.ISupportInitialize)(this.m_CustomersBindingSource)).EndInit();
         this.ResumeLayout(false);
         this.PerformLayout();

      }


      private System.Windows.Forms.TextBox m_ItemTextBox;
      private System.Windows.Forms.DataGridView m_Grid;
      private System.Windows.Forms.Button m_GetItemButton;
      private System.Windows.Forms.BindingSource m_CustomersBindingSource;

      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.Run(new Form1());
      }
   }
23.78.DataBinding DataGrid
23.78.1.Data binding to a programatically created dataset to a DataGridData binding to a programatically created dataset to a DataGrid
23.78.2.Table Create/Row DeleteTable Create/Row Delete
23.78.3.Binding to DataGrid
23.78.4.DataReader Binding
23.78.5.Using a BindingSource to bind a list to a DataGridView control.
23.78.6.Data Binding DataGrid
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.