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[pos] as 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(94, 197);
this.m_ItemTextBox.Name = "m_ItemTextBox";
this.m_ItemTextBox.Size = new System.Drawing.Size(100, 20);
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(12, 12);
this.m_Grid.Name = "m_Grid";
this.m_Grid.Size = new System.Drawing.Size(465, 176);
this.m_Grid.TabIndex = 4;
//
this.m_GetItemButton.Location = new System.Drawing.Point(12, 195);
this.m_GetItemButton.Name = "m_GetItemButton";
this.m_GetItemButton.Size = new System.Drawing.Size(75, 23);
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(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(503, 232);
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());
}
}