本文介绍了如何在Windows应用程序的datagridview中编辑,更新,删除数据详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,先生,现在我正在学习C#语言.所以我想知道如何在datagridview中编辑,更新,删除详细信息.我在sqlserver中将存储过程用作后端的地方.请给我清楚易懂的编码
Hello sir., Now i am learning of c# language. So i want to know about how to edit,update,delete details in datagridview. Where i am using stored procedure in sqlserver as a back end. Kindly give me clearly understandable coding
推荐答案
public partial class DataTrialForm : Form
{
private String connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private String selectQueryString = null;
public DataTrialForm()
{
InitializeComponent();
}
//In the Form Load event set data source for the DataGridView control.
private void DataTraiForm_Load(object sender, EventArgs e)
{
connectionString = ConfigurationManager.AppSettings["connectionString"];
sqlConnection = new SqlConnection(connectionString);
selectQueryString = "SELECT * FROM t_Bill";
sqlConnection.Open();
sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridViewTrial.DataSource = bindingSource;
// if you want to hide Identity column
dataGridViewTrial.Columns[0].Visible = false;
}
//To update, insert or delete data in database from DataGridView have a look at this code snippet.
private void addUpadateButton_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
try
{
dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
希望对您有所帮助.
I hope it will help you.
这篇关于如何在Windows应用程序的datagridview中编辑,更新,删除数据详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!