Reputation: 25
I have a scenario where I can save the name and phone number. but now I want to use my "Save" button as update button from clicking on grid view data.
Also after clicking on gridview data "save" button automatically change as "Update" I want to know if there is any event or something to make it.
Screenshot of my form:
Here is my code:
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 StoredProcedureExample
{
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();
}
}
}
Upvotes: 2
Views: 2062
Reputation: 26
Or you can create an Update button then place it behind the Save button. Add a RowHeaderMouseClick
event on your dataGridView that will place the values from the highlighted cells to the textboxes.
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
}
Then, add these lines to your Update button Click
event to bring the Save button in front again.
saveButton.BringToFront();
updateButton.SendToBack();
Upvotes: 1
Reputation: 298
Make two Event for grid view.
1)Got Focus
2)Lost Focus
1)On GotFocus Event Change it to Update and maintain mode to insert and update
2)On Lost Focus Change it to Save.
Upvotes: 1