using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
DataGrid mygrid;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
string query = "SELECT * FROM Customers";
public Form1()
{
InitializeComponent();
mygrid = new DataGrid();
mygrid.Size = new Size(400,400);
this.Controls.Add(mygrid);
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("Server = .; Database=Northwind;Integrated Security=SSPI");
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(query, con);
ds = new DataSet();
da.Fill(ds, "customers");
mygrid.DataSource = ds.Tables["customers"];
DataTable dt =new DataTable();
DataGridCell cell = mygrid.CurrentCell;
cell.ColumnNumber = 4; //กำหนด column ที่ต้องการดึงข้อมูล
cell.RowNumber = 3;//กำหนด row ที่ต้องการดึงข้อมูล
object obj = mygrid[cell]; //นำค่าของ cell ใน column,row ตามที่กำหนด เก็บไว้ที่ตัวแปร obj
MessageBox.Show(obj.ToString()); //แสดงค่าของตัวแปร obj ออกมา
}
}
}
[With great power comes great responsibility]