VB.NET - update data into datagrid view - Asked By shravan modhavadiya on 18-Jun-12 06:48 AM

i use datagridview for viewing my table's data...how can i update record's data direct from datagrid 
Jitendra Faye replied to shravan modhavadiya on 18-Jun-12 06:56 AM
Use following link for DataGridView Editing-

http://www.visualbuilder.com/showCode.php?id=81373&scd_id=2978
http://code.msdn.microsoft.com/CSWinFormDataGridView-29783221/sourcecode?itemId=6080
I hope this will help you.
bharti odedra replied to shravan modhavadiya on 18-Jun-12 07:19 AM

Please try to search next time. Check the links below.

http://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data

http://stackoverflow.com/questions/520051/c-sharp-issue-how-do-i-save-changes-made-in-a-datagridview-back-to-the-datatabl

http://stackoverflow.com/questions/2623548/winforms-datagridview-update-database

http://stackoverflow.com/questions/4051390/c-sharp-winforms-how-to-send-updates-from-datagridview-to-database

http://stackoverflow.com/questions/3796908/visual-studio-2010-c-sharp-datagridview-save-changes

bharti odedra replied to shravan modhavadiya on 18-Jun-12 07:25 AM
try this
using (SqlDataAdapter ruleTableDA = new SqlDataAdapter("SELECT rule.fldFluteType AS [Flute], rule.fldKnife AS [Knife], 
       rule
.fldScore AS [Score], rule.fldLowKnife AS [Low Knife],
       rule
.fldMatrixScore AS [Matrix Score], rule.fldMatrix AS [Matrix]
       FROM dbo
.tblRuleTypes rule WHERE rule.fldMachine_ID = '1003'", con))
   
{
       
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ruleTableDA);
       
DataTable dt = new DataTable();
        dt
= RuleTable.DataSource as DataTable;
       
//ruleTableDA.Fill(dt);
        ruleTableDA
.Update(dt);
   
}
bharti odedra replied to shravan modhavadiya on 18-Jun-12 07:29 AM
button1_Click(object sender, EventArgs e)
{
   
Validate();
    customerBindingSource
.EndEdit();
    customerTableAdapter
.Update(customerAppDS21.Customer);
}

void grdCustomers_RowLeave(object sender, DataGridViewCellEventArgs e)
{
   
DataGridViewRow gvr = grdCustomers.Rows[e.RowIndex];
   
if (gvr.Cells[0].Value == null)
   
{
       
String g = Guid.NewGuid().ToString();
        gvr
.Cells[0].Value = g;
   
}
   
else
   
{
       
String currID = gvr.Cells[0].Value.ToString();
       
if (currID.Equals(""))
       
{
           
String g = Guid.NewGuid().ToString();
            gvr
.Cells[0].Value = g;
       
}
   
}
}

void grdCustomers_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
   
DataGridViewRow gvr = grdCustomers.Rows[grdCustomers.SelectedCells[0].RowIndex];
   
if (gvr.Cells[0].Value == null)
   
{
       
String g = Guid.NewGuid().ToString();
        gvr
.Cells[0].Value = g;
   
}
   
else
   
{
       
String currID = gvr.Cells[0].Value.ToString();
       
if (currID.Equals(""))
       
{
           
String g = Guid.NewGuid().ToString();
            gvr
.Cells[0].Value = g;
       
}
   
}
}
dipa ahuja replied to shravan modhavadiya on 18-Jun-12 08:37 AM
Check this simple code:
 
Step 1 : add buttons in Form_Load Event
 
private void DataGridUpdate_Load(object sender, EventArgs e)
{
  DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
  //Add update button
  btn.HeaderText = "Click Data";
  btn.Text = "Update";
  btn.Name = "btn";
  btn.UseColumnTextForButtonValue = true;
  dataGridView1.Columns.Insert(0, btn);
 
  //Add delete button
  DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
  btnDelete.HeaderText = "Click Data";
  btnDelete.Text = "Delete";
  btnDelete.Name = "btnDelete";
  btnDelete.UseColumnTextForButtonValue = true;      
}
 
Step 2 :And Implement the CellContentClick Event
 
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  int rowIndex = e.RowIndex;
  int columnIndex = e.ColumnIndex;
      
  string city = this.dataGridView1.Rows[rowIndex].Cells["city"].Value.ToString();
  string id = this.dataGridView1.Rows[rowIndex].Cells["empid"].Value.ToString();        
 
  if (e.ColumnIndex == 0)
  {
    SqlConnection conn = new SqlConnection("ConnectionSTring");
    conn.Open();
 
    SqlCommand comm = new SqlCommand("Update emp set city=@city where empid=@empid", conn);
    comm.Parameters.AddWithValue("city", city);
    comm.Parameters.AddWithValue("empid", id);
    comm.ExecuteNonQuery();
    conn.Close();
 
    MessageBox.Show("Updated!");
     //Re-bind dataGridview
  }
  else if (e.ColumnIndex == 1)
  {
    SqlConnection conn = new SqlConnection("ConnectionString");
    conn.Open();
 
    SqlCommand comm = new SqlCommand("Delete from emp where empid=@empid", conn);
    comm.Parameters.AddWithValue("empid", id);
    comm.ExecuteNonQuery();
    conn.Close();
 
    MessageBox.Show("Deleted!");
     //Re-bind dataGridview
  }
}
 
 
 
shravan modhavadiya replied to bharti odedra on 18-Jun-12 10:04 AM
Thanks For your Reply. but i want do this..........i have windows form....and language is VB.............when user change value of cell in datagrid , this updated value saved on database.....when i reopen this grid view updated record will be shown..........i just want to update database through the datagrid......................Please Give Sollution................