Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello programmers.
i have a problem in updating my data in my datagrid.

i don't use any code.
i just use object datasource to get my data on my database.

my problem is, when i clicked update, nothing happen.


i hope you can help me guys.
thank you.
Posted
Comments
Sandeep Mewara 11-May-11 2:59am    
Until you share your code (creation/implementation of object data-source), it would be difficult to comment.
chaosgray 11-May-11 3:25am    
i don't have codes sir. im just using the datasource.

Hello my friend. After having add the edit command, do this steps:

1.) make this method for binding
void BindGrid()
{
sqlDataAdapter1.Fill( dataSet1 );
DataGrid1.DataSource = dataSet1;
DataGrid1.DataKeyField = "xxxID";
DataGrid1.DataBind();
}

2.) handle this event

private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}

3.) handle this event

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
}

4.) and this event...

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.
DataGridCommandEventArgs e)
{
// Retrieve TextBox Controls from DataGrid
TextBox txtProductName = (TextBox)e.Item.Cells[2].Controls[0];
TextBox txtUnitPrice = (TextBox)e.Item.Cells[3].Controls[0];

// Assign Parameters to SqlCommand
sqlCommand1.Parameters["@field1"].Value= txtField1.Text;
sqlCommand1.Parameters["@field2"].Value = txtField2.Text;
sqlCommand1.Parameters["@xxxID"].Value = DataGrid1.DataKeys[ e.Item.ItemIndex ];

// Execute SqlCommand
sqlConnection1.Open();
sqlCommand1.ExecuteNonQuery();

// Deselect Row for Editing
DataGrid1.EditItemIndex = -1;
BindGrid();
sqlConnection1.Close();
}
 
Share this answer
 
Comments
chaosgray 11-May-11 3:59am    
where will i put this code on?
sorry. im just new to this program.
janwel 11-May-11 4:13am    
on your *.aspx.vb?
chaosgray 11-May-11 4:13am    
are you sure sir?
janwel 11-May-11 4:14am    
yes try it like this
on your page load
dim strSQL as String
Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("connection")     
 strSQL =   "Your Query"    
connection.Open()
Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
GridView1.DataSource = myCommand.ExecuteReader()
GridView1.DataBind()
myCommand.Dispose()
connection.close

Then follow the above codes for manipulating gridview
 
Share this answer
 
Comments
chaosgray 11-May-11 4:18am    
thank you sir.

you are the great one!
janwel 11-May-11 4:18am    
did you understand my instruction ?
chaosgray 11-May-11 4:19am    
a bit.
janwel 11-May-11 4:20am    
mark solved if finish

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900