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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from datagrid to update sql table

Status
Not open for further replies.

gcole

Programmer
Aug 2, 2000
390
US
I have an editable datagrid that allows the user to change values in some fields. When they have completed the changes I need to read from the datagrid. I have seached and can't find anything. Do you read it into an array?

Help is very appreciated.
 
gcole, if it's just to update a db table then you can just add an update query to your data adapter.

Nick
 
I will look into using that. Does a data adapter save the changes from and edited datagrid?
 
I did want to mention that I have a sample based on a dataset that works. The follwoing statement breaks:

if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem)
{
// Get the current row for update or delete operations later.
DataRow dr = PriceChangesDS.Tables["Table"].Rows.Find(dgPriceChanges.DataKeys[di.ItemIndex]);

// Update the row instead.
dr["Email"] = ((TextBox)di.FindControl("mult")).Text;
}


I don't have any di.ItemIndex. Do you know what is setup wrong in the datagtrid?
 
if it's just to update a db table then you can just add an update query to your data adapter

*Shudder* - By doing this you are allowing your GUI to affect your database directly. This is fine if you're writing an application to store you grandma's DVD collection. If you are writing an application that is to be used in a real situation then this method is bad. By using a TableAdapter you are bypassing the Business layer and not abstracting the data layer. What happens if you need to switch from SQL Server to MySQL or Oracle? You have to rewrite all your code if it's not abstracted.

You can loop through the rows of a DataTable which is the DataSource of the datagrid (I'm guessing) and see what the row state is. If it is changed, added, or deleted then apply those changes using your data access layer.

 
Updating a dataset/table is not the same as updating the underlying database :)

Have a look at this:
Hope this helps,

Alex

[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
This is the solution that I have worked out.
I loop through the datagird:
foreach (DataGridItem di in dgPriceChanges.Items)

Set the selected index:
datagrid.SelectedIndex = cnt;

Then I read the control(textbox) in the cell:
di.Cells[8].Controls[1]

I hope this helps someone else as I had a hard time finding info on it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top