DataGrid easy Edit-Update using ADO.NET/DataGrid functionality C#?

K

Kostia

Hi, so I am using built in Edit, Delete, Update i.e.
OnEditCommand="DataGrid1_Edit" OnUpdateCommand="DataGrid1_Update" with
handler
question below in regards to the actual update after edit

//called by the handler deletes row no problem and updates DB no
problem
private void DeleteItem(DataGridCommandEventArgs e)
{
DataRow dr = dsEmployees1.Tables["Employees"].Rows[e.Item.ItemIndex];
dr.Delete();
sqlDataAdapter1.Update(dsEmployees1, "Employees");
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}

public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs E)
{
DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
DataGrid1.DataBind();
}

//NOW WHAT DO I NEED TO DO HERE TO ACTUALLY UPDATE THE ROW?
//IS THE ONLY WAY TO DO IT IS by getting individual column values and
//constructing SQL statement? IS there a way to call some kind of
update to accept and commit entered changes? done through Edit?
public void DataGrid1_Update(Object sender, DataGridCommandEventArgs
E)
{
DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
DataRow dr = dsEmployees1.Tables["Employees"].Rows[DataGrid1.EditItemIndex];
sqlDataAdapter1.Update(dsEmployees1, "Employees");
// Rebind the data source to refresh the DataGrid control.
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
 
S

Scott Mitchell [MVP]

//NOW WHAT DO I NEED TO DO HERE TO ACTUALLY UPDATE THE ROW?
//IS THE ONLY WAY TO DO IT IS by getting individual column values and
//constructing SQL statement?


Yes. This is precisely what you have to do. For a look at an article
that builds an editable DataGrid, see:

An Extensive Examination of the DataGrid Web Control: Part 6
http://aspnet.4guysfromrolla.com/articles/071002-1.aspx

Happy Programming!

--

<shameless plug>
For more information on the DataGrid, DataList, and Repeater controls,
consider picking up a copy of my latest book:

ASP.NET Data Web Controls
http://www.amazon.com/exec/obidos/ASIN/0672325012/4guysfromrollaco
</shameless plug>

Happy Programming!

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top