且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#.net windows应用程序中使用datagridview插入,更新和删除数据?

更新时间:2023-10-19 17:20:46

how-to-insert-edit-update-and-delete.html [ ^ ]



检查链接..希望它会有所帮助..


查看这篇文章..

插入,更新&安培;从C#.Net 中的DataGridView中删除表[ ^ ]


如何使用DataGridView插入,编辑,更新和删除数据在Windows窗体C#.net ||在Windows窗体中插入,更新和删除DataGridView C#.net

标签:C#.net,GridView,Windows应用程序



在本文中,我将展示使用DataGridview插入,编辑,更新和删除选项。

为此,我设计的表单包含两个带有名称和位置的文本框,DataGridview用于显示数据和保存,编辑,更新和删除四个按钮。

要执行此操作,请按照以下步骤操作:

•在表单加载中我绑定数据库中的数据。

•在保存按钮单击事件中将数据保存到数据库,该数据插入到名称和位置tex中tbox。

•在删除按钮单击事件中从数据库中删除DataGridview中的选定行数据。

•在编辑按钮中单击事件将所选数据从Gridview填充到名称和位置文本框。

•在更新按钮中单击事件更新数据,编辑名称和位置文本框。

在Form.cs中编写以下代码:

Form.cs代码:



使用系统;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;

使用System.Configuration;

使用System.Data.SqlClient;


名称空间保存数据

{

公共部分类Form1:表格

{


SqlConnection con = newSqlConnection(Configurat ionManager.ConnectionStrings [Sqlcon] .ConnectionString);

public Form1()

{

InitializeComponent();

Bind();



}



private void清除()

{

txtName.Text = string.Empty;

txtLocation.Text = string.Empty;

}



private void btnSave_Click(object sender,EventArgs e)

{

con.Open();

SqlCommand cmd = new SqlCommand(Insert into Test_Data(Name,Location)Values(@ Name,@ Location),con);

cmd.Parameters.AddWithValue(Name,txtName。文字);

cmd.Parameters.AddWithValue(Location,txtLocation.Text);

cmd.ExecuteNonQuery();

con。关闭();

MessageBox.Show(成功插入);

Bind();

清除();

}



private void Bind()

{

con.Open();

SqlDataAdapter da = new SqlDataAdapter(select * from Test_Data,con);

DataTable dt = new DataTable();

da.Fill(dt);

dataGridView1.DataSource = dt;

con.Close();

}



private void btnDelete_Click(object sender,EventArgs e)

{

SqlCommand delcmd = new SqlCommand();

if(dataGridView1.Rows.Count> 1 && dataGridView1.SelectedRows [0] .Index!= dataGridView1.Rows.Count - 1)

{

delcmd.CommandText =DELETE FROM Test_Data WHERE ID =+ dataGridView1.SelectedRows [0] .Cells [0] .Value.ToString()+;

con.Open();

delcmd.Connection = con;

delcmd.ExecuteNonQuery();

con.Close();

dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows [0] .Index) ;

MessageBox.Show(行已删除);

}

Bind();

}


private void btnUpdate_Click(object sender,EventArgs e)

{

con.Open();

SqlCommand cmd = new SqlCommand(Update Test_Data set Name = @ Name,Location = @ Location Where(Name = @ Name),con);

cmd.Parameters.AddWithValue (@ Name,txtNa me.Text);

cmd.Parameters.AddWithValue(@ Location,txtLocation.Text);

cmd.ExecuteNonQuery();

MessageBox.Show(更新......);

con.Close();

Bind();

清除();

}



private void btnEdit_Click_1(对象发件人,EventArgs e)

{

int i;

i = dataGridView1.SelectedCells [0] .RowIndex;

txtName.Text = dataGridView1.Rows [i] .Cells [1] .Value.ToString();

txtLocation.Text = dataGridView1.Rows [i] .Cells [2] .Value.ToString();

}

}

}





然后运行应用程序,您将获得如下输出:

Hello,

How to insert,Update and Delete data using datagridview in c# .net windows application?
It should be recede or rearrange ID or Sr. No. after delete any row.

How it can be possible?

Please help me.

Thanks in Advance

Regards
Ankit Agarwal
Software Engineer

how-to-insert-edit-update-and-delete.html[^]

Check the link..hope it will help..


Check this article..
Insert, Update & Delete Table from DataGridView in C#.Net[^]


How to Insert, Edit, Update and Delete Data with DataGridView in Windows Form C#.net ||Inserting , Updating and Deleting with DataGridView in Windows forms C#.net
Labels: C#.net, GridView, Windows Applications

In this article I am showing to Inserting , Editing , Updating and Deleting options with DataGridview.
For that I am Designing form with two textboxes with Name and Location ,DataGridview to display data and four buttons to Save , Edit , Update and Delete.
To do this just follow below steps:
• In form load I am binding the data from database.
• In save button click event saving data to database which are inserted into the name and location textboxes.
• In Delete button click event Deleting the selected row data in DataGridview from database.
• In Edit button Click event filling the selected data from Gridview into Name and location textboxes.
• In Update Button click event updating data which are edited the name and location textboxes.
Write the following code in Form.cs :
Form.cs Code :

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

namespace savedata
{
public partial class Form1 : Form
{

SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["Sqlcon"].ConnectionString);
public Form1()
{
InitializeComponent();
Bind();

}

private void Clear()
{
txtName.Text = string.Empty;
txtLocation.Text = string.Empty;
}

private void btnSave_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert Into Test_Data(Name,Location) Values (@Name,@Location)", con);
cmd.Parameters.AddWithValue("Name", txtName.Text);
cmd.Parameters.AddWithValue("Location", txtLocation.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted sucessfully");
Bind();
Clear();
}

private void Bind()
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Test_Data", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}

private void btnDelete_Click(object sender, EventArgs e)
{
SqlCommand delcmd = new SqlCommand();
if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
{
delcmd.CommandText = "DELETE FROM Test_Data WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
con.Open();
delcmd.Connection = con;
delcmd.ExecuteNonQuery();
con.Close();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
MessageBox.Show("Row Deleted");
}
Bind();
}

private void btnUpdate_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Update Test_Data set Name=@Name,Location=@Location Where(Name=@Name)", con);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("updated......");
con.Close();
Bind();
Clear();
}

private void btnEdit_Click_1(object sender, EventArgs e)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
txtName.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txtLocation.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
}
}
}


Then run the application you will get output like below: