.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

how can we update database thru datagrid

Posted By: Sunil     Posted Date: September 19, 2008    Points:2   Category :Windows Application
hi friends,

when User makes changes in datagridview and clicks save or update button the changes which are made in datagridview should get reflect in table in databse.
this is for using in windows application

Author: Faizal             
Posted Date: September 19, 2008     Points: 5   

Try this code

public partial class MainForm: Form {
public MainForm() {

private void MainForm_Load(
object sender, EventArgs e)
// resize the column once, but allow the
// users to change it.

//tracks for PositionChanged event last row
private DataRow LastDataRow = null;

/// Checks if there is a row with changes and
/// writes it to the database
/// </SUMMARY>
private void UpdateRowToDatabase() {
if (LastDataRow!=null) {
if (LastDataRow.RowState==
DataRowState.Modified) {

private void regionBindingSource_PositionChanged(
object sender, EventArgs e)
// if the user moves to a new row, check if the
// last row was changed
BindingSource thisBindingSource =
DataRow ThisDataRow=
if (ThisDataRow==LastDataRow) {
// we need to avoid to write a datarow to the
// database when it is still processed. Otherwise
// we get a problem with the event handling of
//the DataTable.
throw new ApplicationException("It seems the" +
" PositionChanged event was fired twice for" +
" the same row");

// track the current row for next
// PositionChanged event
LastDataRow = ThisDataRow;

private void MainForm_FormClosed(
object sender, FormClosedEventArgs e)

Author: Santosh             
Posted Date: October 22, 2009     Points: 5   

Like one textbox velue u r passing in database in save click
Synatx :
Dim objNameParam As New SqlParameter("@Name", SqlDbType.varchar)
objNameParam.Direction = ParameterDirection.Input
objNameParam.Value = textbox1.text

same like pass the grid view cell value to database in save click

Syntax :
Dim objNameParamAs New SqlParameter("@Name", SqlDbType.varchar)
objNameParam.Direction = ParameterDirection.Input
objNameParam.Value = DataGridView1.row(0).cell("PassingColumnName").value.ToString()

This is for limited values
i will try write code for multi insert and post it Soon.


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend