2017-11-18 4 views
1

名前と電話番号を保存できるシナリオがあります。しかし、私は、グリッドビューのデータをクリックすることから、 "保存"ボタンを更新ボタンとして使用したいと思っています。 また、gridviewデータをクリックした後、「保存」ボタンが自動的に「更新」として変更されます イベントを作成するかどうかを知りたい場合があります。ここ保存と編集の両方に1つのボタンを使用してください。#

マイフォーム画像

enter image description here

は私のコード -

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace StoreProcedureExample 
{ 
    public partial class Form1 : Form 
    { 
     SqlConnection conn = new SqlConnection("Data Source=DESKTOP-S80LS9D;Initial Catalog=ComboBoxTest;Integrated Security=True"); 
     SqlCommand cmd; 
     SqlDataAdapter data; 
     DataSet ds; 
     int num = 0; 
     int i; 

     public Form1() 
     { 
      InitializeComponent(); 
      cmd = new SqlCommand("UserInformation", conn); 
      data = new SqlDataAdapter(cmd); 
      ds = new DataSet(); 
      data.Fill(ds); 
      this.userdataGridView1.DataSource = ds.Tables[0]; 
     } 

     private void saveButton_Click(object sender, EventArgs e) 
     { 
      SaveRecord(); 
      ShowRecordAfterSaveAndEdit(); 
      clearfilds(); 
     } 

     public void SaveRecord() 
     { 
      cmd = new SqlCommand("insertupdate", conn); 
      cmd.CommandType = CommandType.StoredProcedure; 
      if (userTextBox.Text == "") 
      { 
       num = 0; 
      } 
      else 
      { 
       num = int.Parse(userTextBox.Text); 
      } 
      cmd.Parameters.AddWithValue("@id", num); 
      cmd.Parameters.AddWithValue("@name", nameTextBox.Text); 
      cmd.Parameters.AddWithValue("@PhoneNumber", phoneNumberTextBox.Text); 
      if (saveButton.Text == "Save") 
      { 
       cmd.Parameters.AddWithValue("@Flag", 'S'); 
      } 
      else 
      { 
       cmd = new SqlCommand("UpdateUser", conn); 
       cmd.CommandType = CommandType.StoredProcedure; 
      } 

      try 
      { 
       conn.Open(); 
       if (cmd.ExecuteNonQuery() > 0 && saveButton.Text == "Save") 
       { 
        MessageBox.Show("record inserted"); 
       } 
       else 
       { 
        MessageBox.Show("record Update"); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
       conn.Close(); 
      } 
     } 

     public void clearfilds() 
     { 
      userTextBox.Text = ""; 
      nameTextBox.Text = ""; 
      phoneNumberTextBox.Text = ""; 
      saveButton.Text = "Save"; 
     } 

     public void ShowRecordAfterSaveAndEdit() 
     { 
      cmd = new SqlCommand("UserInformation", conn); 
      data = new SqlDataAdapter(cmd); 
      ds = new DataSet(); 
      data.Fill(ds); 
      this.userdataGridView1.DataSource = ds.Tables[0]; 
      conn.Close(); 
     } 

     private void ShowData(object sender, EventArgs e) 
     { 
      int show = userdataGridView1.CurrentRow.Index; 
      userTextBox.Text = userdataGridView1.CurrentRow.Cells["Id"].Value.ToString(); 
      nameTextBox.Text = userdataGridView1.CurrentRow.Cells["Name"].Value.ToString(); 
      phoneNumberTextBox.Text = userdataGridView1.CurrentRow.Cells["PhoneNumber"].Value.ToString(); 
     } 
    } 
} 
+1

、それを変更...何のイベントが – pryashrma

+1

が、私はこのキャプション TIA – nazrul

+1

saveButton.Text =「アップデート」を変更する方法を知りたい必要ありません。 – pryashrma

答えて

1

また、[更新]ボタンを作成して[保存]ボタンの後ろに配置することもできます。強調表示されたセルの値をテキストボックスに配置するdataGridViewに​​イベントを追加します。

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     foreach (DataGridViewRow row in dataGridView1.SelectedRows) 
     { 
      //assuming the datagrid displays ID in the 1st cell, Cells[0] 
      string name = row.Cells[1].Value.ToString(); 
      string phoneNum = row.Cells[2].Value.ToString(); 
     } 
     textBox2.Text = name; 
     textBox3.Text = phoneNum; 
     updateButton.BringToFront(); 
     saveButton.SendToBack(); //optional 
    } 

はその後、再び前に[保存]ボタンをもたらすために、あなたの更新ボタンを Clickイベントをこれらの行を追加します。あなたはボタンのキャプションを変更したい場合は

saveButton.BringToFront(); 
updateButton.SendToBack(); 
+0

Thaxs @C C it works .. ダブルクリックイベントで解決しますが – nazrul

1

は、グリッドビューのための2つのイベントを作成しています。

1)ガットフォーカス

のGotFocusイベントで

1フォーカス

)を紛失

2)を挿入し、

ロスト

2)フォーカスの変更を更新するために更新し、モードを維持するために、それを変更します保存する。

関連する問題