IT_Programming/C# 모바일

DataGrid에서 선택한 값을 불러 오기 1-2

JJun ™ 2006. 5. 23. 16:02

[ 전체 소스 ]

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Data;
using System.Data.SqlClient;

 

namespace SmartDeviceApplication1
{
 /// <summary>
 /// Form3에 대한 요약 설명입니다.
 /// </summary>
 public class Form3 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
 
  public Form3()
  {
   
//
   // Windows Form 디자이너 지원에 필요합니다.
   //
   InitializeComponent();

   //
   // TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
   //
  }

  /// <summary>
  /// 사용 중인 모든 리소스를 정리합니다.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   base.Dispose( disposing );
  }

 

  #region Windows Form 디자이너에서 생성한 코드
  
/// <summary>
  /// 디자이너 지원에 필요한 메서드입니다.
  /// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
  /// </summary>
  private void InitializeComponent()
  {
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   
//
   // dataGrid1
   //
   this.dataGrid1.Size = new System.Drawing.Size(240, 104);
   this.dataGrid1.Text = "dataGrid1";
   this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
   
//
   // button1
   //
   this.button1.Location = new System.Drawing.Point(152, 232);
   this.button1.Text = "추가";
   this.button1.Click += new System.EventHandler(this.button1_Click);
  
 //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(88, 128);
   this.textBox1.Size = new System.Drawing.Size(136, 21);
   this.textBox1.Text = "";
   
//
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(88, 160);
   this.textBox2.Size = new System.Drawing.Size(136, 21);
   this.textBox2.Text = "";
   
//
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(88, 192);
   this.textBox3.PasswordChar = '*';
   this.textBox3.Size = new System.Drawing.Size(136, 21);
   this.textBox3.Text = "";
  
 //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 128);
   this.label1.Size = new System.Drawing.Size(64, 20);
   this.label1.Text = "이름";
  
 //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(8, 160);
   this.label2.Size = new System.Drawing.Size(64, 20);
   this.label2.Text = "주소";
  
 //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(8, 192);
   this.label3.Size = new System.Drawing.Size(64, 20);
   this.label3.Text = "패스워드";
   
//
   // Form3
   //
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.dataGrid1);
   this.Text = "Form3";
   this.Load += new System.EventHandler(this.Form3_Load);

  }
  #endregion

 

  private void Form3_Load(object sender, System.EventArgs e)
  {
     D_grid();
  }

  

  private void button1_Click(object sender, System.EventArgs e)
  {
     string _name = this.textBox1.Text;
     string _addr = this.textBox2.Text;
     string _pwd = this.textBox3.Text;

   

     string constr = "Server = 203.241.202.71; DataBase = Mobile; uid = sa; pwd = 1234";
     SqlConnection setdb = new SqlConnection(constr);
     setdb.Open();

   

     string sql_02 = "insert into member(name, addr, pwd)

                           values('" + _name + "','" + _addr + "','" + _pwd +"')";


     SqlCommand cmd = new SqlCommand(sql_02,setdb);
     cmd.ExecuteNonQuery();

   

     this.textBox1.Text = "";
     this.textBox2.Text = "";
     this.textBox3.Text = "";

   

     D_grid();
  }

  

  private void D_grid()
  {
    string constr = "Server = 203.241.202.71; DataBase = Mobile; uid = sa; pwd = 1234";
   
    SqlConnection setdb = new SqlConnection(constr);
    setdb.Open();

   

    string sql_01 = "select * from Member";
    SqlDataAdapter da = new SqlDataAdapter(sql_01, setdb);
    DataSet ds = new DataSet();
    
    DataTable dt = new DataTable();
    DataRow dr;
    
    dt.Columns.Add(new DataColumn("D_idx", typeof(string)));
    dt.Columns.Add(new DataColumn("D_name", typeof(string)));
    dt.Columns.Add(new DataColumn("D_addr", typeof(string)));

    da.Fill(ds, "M_Member");
    
    try
    {
        for(int i=0; i <= ds.Tables["M_Member"].Rows.Count; i++)
       {
          dr = dt.NewRow();

          dr["D_idx"] = ds.Tables["M_Member"].Rows[i]["idx"].ToString();
          dr["D_name"] = ds.Tables["M_Member"].Rows[i]["name"].ToString();
          dr["D_addr"] = ds.Tables["M_Member"].Rows[i]["addr"].ToString();

          dt.Rows.Add(dr);
       }
    }
    catch(Exception ex)
    {

    }

   

   setdb.Close();

   

   DataView dv = new DataView(dt);
   this.dataGrid1.DataSource = dv;
  
 //this.dataGrid1.DataSource = ds.Tables[0];
  }

  

  private void dataGrid1_Click(object sender, System.EventArgs e)
  {
     MessageBox.Show ("Row is " + dataGrid1.CurrentCell.RowNumber
      + ", Col is " + dataGrid1.CurrentCell.ColumnNumber
      + ", Value is " + dataGrid1[ dataGrid1.CurrentCell.RowNumber,       

     dataGrid1.CurrentCell.ColumnNumber] );
   
     this.textBox1.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString();
     this.textBox2.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString();
  }

 }
}