.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

จะดึงค่าแต่ละ cell ของ DataGrid ทำยังไงครับ Options · View
tanawatt
Posted: Thursday, February 28, 2008 3:09:10 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 2/28/2008
Posts: 5
Location: thai

จะดึงค่าแต่ละ cell ของ DataGrid ทำยังไงครับ ต้องการดึงค่าบาง cell มาคำนวณน่ะครับ

paedotnet
Posted: Saturday, March 01, 2008 12:36:00 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk
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]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by