IT_Programming/C# 모바일

DataGrid에서 선택한 값을 불러 오기 2 (프로퍼티 사용)

JJun ™ 2006. 5. 23. 16:17
 

[ Form1.cs ]

 

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

using System.Data.SqlClient;

 

namespace SmartDeviceApplication3
{
 /// <summary>
 /// Form1에 대한 요약 설명입니다.
 /// </summary>


 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;

  public string constr=@"Server=***.***.***.***;DataBase=mobile;uid=***;password=****";

  

  public Form1()
  {
   
//
   // 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.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.button1 = new System.Windows.Forms.Button();
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   
//
   // button1
   //
   this.button1.Location = new System.Drawing.Point(176, 248);
   this.button1.Size = new System.Drawing.Size(72, 24);
   this.button1.Text = "추가";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   
//
   // dataGrid1
   //
   this.dataGrid1.Size = new System.Drawing.Size(304, 200);
   this.dataGrid1.Text = "dataGrid1";
   //this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
   this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
   
//
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(40, 216);
   this.textBox1.Text = "textBox1";
  
 //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(40, 248);
   this.textBox2.Text = "textBox2";
   
//
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(152, 216);
   this.textBox3.Text = "textBox3";
   
//
   // Form1
   //
   this.ClientSize = new System.Drawing.Size(330, 383);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.dataGrid1);
   this.Controls.Add(this.button1);
   this.Menu = this.mainMenu1;
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);

  }
  #endregion

  /// <summary>
  /// 해당 응용 프로그램의 주 진입점입니다.
  /// </summary>

 

  static void Main()
  {
     Application.Run(new Form1());
  }

  
  private void Form1_Load(object sender, System.EventArgs e)
  {
     DataSet();
  }
  
  private void DataSet()
  {
   
      SqlConnection setdb = new SqlConnection(constr);
      try
      {
         setdb.Open();

         string sql = "Select * from member";
         SqlDataAdapter da = new SqlDataAdapter(sql, setdb);
         DataSet ds = new DataSet();

         DataTable dt = new DataTable();
         DataRow dr;  

 

         dt.Columns.Add(new DataColumn("idx",typeof(int)));
         dt.Columns.Add(new DataColumn("name", typeof(string)));
         dt.Columns.Add(new DataColumn("addr", typeof(string)));
  

         da.Fill(ds, "member");

 

         for(int i=0;i<ds.Tables[0].Rows.Count;i++)
         {
             dr = dt.NewRow();
             dr["idx"] = ds.Tables[0].Rows[i]["idx"].ToString();
             dr["name"] = ds.Tables[0].Rows[i]["name"].ToString();
             dr["addr"] = ds.Tables[0].Rows[i]["addr"].ToString();
             dt.Rows.Add(dr);  
        }
   

       DataView dv = new DataView(dt);
       this.dataGrid1.DataSource = dv;
     }
     catch(Exception ee)
     {
        MessageBox.Show(ee.Message.ToString());
     }
     finally
     {
        setdb.Close();
     }
  }

  

  private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
  {
     DataGrid dataGrid=(DataGrid)(sender);

     int k = this.dataGrid1.CurrentRowIndex;
    

     this.textBox1.Text=dataGrid[k,0].ToString();
     this.textBox2.Text=dataGrid[k,1].ToString();
     this.textBox3.Text=dataGrid[k,2].ToString();

  }

  

  private void button1_Click(object sender, System.EventArgs e)
  {
   //   DataGrid dataGrid=(DataGrid)(sender);
   //   int k=this.dataGrid1.CurrentRowIndex;
   //   this.textBox1.Text=dataGrid[k,0].ToString();
   //   this.textBox2.Text=dataGrid[k,1].ToString();
   //   this.textBox3.Text=dataGrid[k,2].ToString();

  

     Form2 frm2=new Form2();
     frm2.Show();
     frm2.pidx=this.textBox1.Text.ToString();
  
//   frm2.pname=this.textBox2.Text.ToString();
 
//   frm2.paddr=this.textBox3.Text.ToString();
  }


 
 }
}

=======================================================================================

 

[ Form2.cs ]

 

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

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

namespace SmartDeviceApplication3
{
 /// <summary>
 /// Form2에 대한 요약 설명입니다.
 /// </summary>


 public class Form2 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  
  string strpidx="";

  public string constr=@"Server=***.***.***.***;DataBase=mobile;uid=***;password=****";
 
  public string pidx
  {
    set
    {
       strpidx=value;
    }
  }

 

//  public string pname
//  {
//   set
//   {
//    this.textBox2.Text =value;
//   }
//  }
//  public string paddr
//  {
//   set
//   {
//    this.textBox3.Text =value;
//   }
//  }

 


  public Form2()
  {
  
 //
   // 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.textBox2 = new System.Windows.Forms.TextBox();
     this.textBox3 = new System.Windows.Forms.TextBox();
     this.label2 = new System.Windows.Forms.Label();
     this.label3 = new System.Windows.Forms.Label();
     this.button1 = new System.Windows.Forms.Button();
     this.button2 = new System.Windows.Forms.Button();
    
//
     // textBox2
     //
     this.textBox2.Location = new System.Drawing.Point(136, 16);
     this.textBox2.Text = "";
    
//
     // textBox3
     //
     this.textBox3.Location = new System.Drawing.Point(136, 48);
     this.textBox3.Text = "";
    
//
     // label2
     //
     this.label2.Location = new System.Drawing.Point(8, 16);
     this.label2.Text = "이름";
    
//
     // label3
     //
     this.label3.Location = new System.Drawing.Point(8, 48);
     this.label3.Text = "주소";
     
//
     // button1
     //
     this.button1.Location = new System.Drawing.Point(40, 128);
     this.button1.Size = new System.Drawing.Size(80, 24);
     this.button1.Text = "수정";
    
//
     // button2
     //
     this.button2.Location = new System.Drawing.Point(136, 128);
     this.button2.Size = new System.Drawing.Size(80, 24);
     this.button2.Text = "삭제";
    
//
     // Form2
     //
     this.ClientSize = new System.Drawing.Size(282, 271);
     this.Controls.Add(this.button2);
     this.Controls.Add(this.button1);
     this.Controls.Add(this.label3);
     this.Controls.Add(this.label2);
     this.Controls.Add(this.textBox3);
     this.Controls.Add(this.textBox2);
     this.Text = "Form2";
     this.Load += new System.EventHandler(this.Form2_Load);

  }
  #endregion

 

  private void Form2_Load(object sender, System.EventArgs e)
  {
     DataView();
  }

  

  private void DataView()
  {
   //this.textBox1.Text=strpidx.ToString();

   SqlConnection setdb = new SqlConnection(constr);
   try
   {
      setdb.Open();

      string sql = "Select * from member where idx= '"+strpidx+"' ";
      SqlCommand cmd=new SqlCommand(sql,setdb);
      SqlDataReader rs=cmd.ExecuteReader();

      while(rs.Read())
      {
         this.textBox1.Text=rs["idx"].ToString();
         this.textBox2.Text=rs["name"].ToString();
         this.textBox3.Text=rs["addr"].ToString();
      }
      rs.Close();
   }
   catch(Exception ee)
   {
      MessageBox.Show(ee.Message.ToString());
   }
   finally
   {
      setdb.Close();
   }
  }
 }
}
=======================================================================================