使用一个按钮来保存和编辑Gridview在c#
问题描述:
我有一个场景,我可以保存名称和电话号码。但现在我想使用我的“保存”按钮作为点击网格视图数据的更新按钮。 同样在点击gridview数据后“保存”按钮会自动更改为“更新” 我想知道是否有任何事件或其他事情可以做到。使用一个按钮来保存和编辑Gridview在c#
我的表单图像
这里是我的代码 -
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();
}
}
}
答
或者您可以创建一个更新按钮,然后将其放在保存按钮后面。在您的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它的工作.. 虽然我解决它与双击事件 – nazrul
答
做了两个事件为网格视图。
1)得到了重点
2)失去焦点
1)在的GotFocus事件将其更改为更新和维护模式,插入和更新
2)失去焦点变化它来保存。
如果要更改按钮的标题,然后改变它......任何情况下需要 – pryashrma
我想知道如何改变这种标题 TIA – nazrul
saveButton.Text =“更新”; – pryashrma