Hi friends,
I have a problem with datagrid update command. When I update a row in datagrid it is getting updated in the database but is not visible in the Datagrid immediately.Only When I click the update command the new value is seen in the datagrid. When I debug and return to the browser the new value is getting bound,there is no need of clicking the update command to see the new value in the grid.How could this happen? this is my code : protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { binddata(); } } protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = e.Item.ItemIndex; binddata(); } protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e) { TextBox Id = (TextBox)e.Item.Cells[1].Controls[0]; TextBox State = (TextBox)e.Item.Cells[2].Controls[0]; TextBox places = (TextBox)e.Item.Cells[3].Controls[0]; con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb"); con.Open(); int idno; idno = Convert.ToInt32(Id.Text); String update = "Update State set Places = '" + places.Text + "' where Id ='" + idno + "'"; cmd = new OleDbCommand (update, con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex = -1; binddata(); } protected void binddata() { con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb"); con.Open(); String state = "Select * from State"; da = new OleDbDataAdapter(state, con); ds = new DataSet(); da.Fill(ds); DataGrid1.DataSource = ds; DataGrid1.DataBind(); } protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = -1; binddata(); } }
|
| Author: Abhay 10 Oct 2008 | Member Level: Diamond | Rating:  Points: 6 |
add a line code and try
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e) { TextBox Id = (TextBox)e.Item.Cells[1].Controls[0]; TextBox State = (TextBox)e.Item.Cells[2].Controls[0]; TextBox places = (TextBox)e.Item.Cells[3].Controls[0]; con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb"); con.Open(); int idno; idno = Convert.ToInt32(Id.Text); String update = "Update State set Places = '" + places.Text + "' where Id ='" + idno + "'"; cmd = new OleDbCommand (update, con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex = -1; DataGrid1.columns.clear();/// add this and try binddata(); }
Thanks and Regards, Abhay
|
| Author: Anitha 10 Oct 2008 | Member Level: Bronze | Rating:  Points: 2 |
Hi,
did u give "datakeynames" property of the datagrid as primary key field of the database.
all yr code are correct.
|
| Author: Palanivel 10 Oct 2008 | Member Level: Silver | Rating:  Points: 3 |
This is very basic thing. Datagrid displays static contents only. so whenever you update the database you need to bind the datagrid. so you just call binddata(); method to solve this problem. this may usefull for pagination also
|
| Author: Palanivel 10 Oct 2008 | Member Level: Silver | Rating:  Points: 3 |
This is very basic thing. Datagrid displays static contents only. so whenever you update the database you need to bind the datagrid. so you just call binddata(); method to solve this problem. this may usefull for pagination also
|
| Author: Cathrin 10 Oct 2008 | Member Level: Gold | Rating:  Points: 3 |
Hi Anitha, I have not used datakeynames property..Will that be a cause of my problem..How should I use the datakeynames property?Could You give me an example?
|
| Author: Cathrin 10 Oct 2008 | Member Level: Gold | Rating:  Points: 1 |
Hi Palani, Thanks for replying .I have bound the datagrid after updating in the database na?
|