Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update DataTable in DataGrid

Status
Not open for further replies.

Eli20

Programmer
Oct 30, 2003
119
0
0
MX
hi, this should be a simple question, but i have never been able to solve the problem.

I have a DataTable, and im binding it to a DataGrid like this:

Code:
this.DataGrid1.DataSource = Tabla;
this.DataGrid1.DataBind();

now, im trying to make the datagrid editable, so i followed a tutorial from MSDN, and i set the EditCommand and it works fine. i get textboxes to edit my values.

but im having trouble with the updateCommand, because i dont know how to update the datatable with the new values without having to go to update directly the original source and then load the datatable again.

can anybody help me?
i hope i made myself clear...

thank you very much

Eli
 
Basically, your goal is to do this:

1. Get the original DataTable.
2. Update a record in the Table.
3. Send the update to the DB.
4. Rebind the grid with the updated table.


The order of steps 2 and 3 can vary according to your approach.

As far as getting the original DataTable goes, you may either 1. re-retrive the data from the DB or 2. cache the data the first time you retrieve it from the DB and use it.

If you're going for option 2, here's a quick example of how you might set it up:

Code:
private DataTable GetData()
{
    DataTable dt = Cache["MyData"] as DataTable;

    if( dt == null )
    {
       dt = //get data from database
    }

    return dt;
}

This will save you the trip to the database.
 
but what i dont want it to go to the database each time.. i can sotre the datatable in the cache like You said, but then how do i update it? so it shows the changes in the datagrid??

thank you

Eli
 
Re-bind the grid using the DataSet you stored in the cache (after you update the DataSet).

For the actual updating, you can either send a single sproc/statement directly to the DB and update it in the DataSet (look at DataKeys in the grid, and PrimaryKey in the DataSet), or you can use a DataAdapter to perform several updates at once based on changes to the DataSet.
 
and then we go back to the original question.. how do i update the dataset??


thank you

Eli
 
hi, thank you very much for your help.. but it still doesnt answer my question.

The example shows how to delete a row.. i dont have any trouble tthat, because i just have to say the index to delete.

but how do i update the datatable???

lets say i update the data in my grid, of row 3, with an id 3.

and i have Name, addess, and phone.

so, i change the Name textbox, and i change the address texbox...
what code whould i put in the UpdateCommand of my DataGrid?

thank you

Eli
 
in the edit command
Code:
dim T as datatable = directcast(sender, datagrid).datasource
Dim Name as string = directcast(e.item.cell(#).contorls(0), textbox).text
Dim Address as string = directcast(e.item.cell(#).contorls(0), textbox).text
dim phone as string = directcast(e.item.cell(#).contorls(0), textbox).text

with T
  .rows(e.item.itemindex).columns("Name") = Name
  .rows(e.item.itemindex).columns("Address") = Address
  .rows(e.item.itemindex).columns("Phone") = Phone
end with

'if needed
'  1. udpate the database
'  2. cache the datatable

with directcast(sender, datagrid)
  .datasource = T
  .datakeyfield = T.columns(#).columnname
  .databind()
end with


Jason Meckley
Database Analyst
WITF
 
This returns a DataRow:

row = dt.Rows.Find(ItemsGrid.DataKeys(e.Item.ItemIndex))

Whereas the example calls the Remove() method, all you'd have to do to update after finding it is this:

row("field_name1") = object
row("field_name2") = anotherObject
 
what code whould i put in the UpdateCommand of my DataGrid"

//update command

TextBox updated = ((TextBox)e.Item.Controls[/*index*/]);
//...etc.

DataRow dr = myDataTable.Rows.Find(ItemsGrid.DataKeys[e.Item.ItemIndex]);

dr["whatever"] = updated.Text;
//...etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top