PC Review


Reply
Thread Tools Rate Thread

DataGrid - clever way to update DataSet

 
 
st
Guest
Posts: n/a
 
      15th Jun 2005
Hi,

I populate a DataGrid programmatically from a DataSet that has its
DataAdapter SQL built dynamically, i.e. the DataGrid contents, field
names and field count is not fixed.

I am at the point where I need to write code to update the bound
DataSet contents to reflect DataGrid row edit (singular) and I am
looking for a slick way to do this. Simplistically, something along the
lines of ds(dgindex) = dg(dgindex).

Is this possible? Would I need to ascertain ds field names and
iterate/correspond columns in the DataGrid.

I am working in C#.

Many thanks,

Simon

 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      15th Jun 2005
This is not as easy as you may think (or want it to be)...

The row index of the row in the DataGrid that you are editing is not
necessarily the same row index in your DataSet. What makes matters a bit
more confusing is that if you allow paging and/or sorting in your grid, the
2 index values may be way off from each other.

You must essentially query the dataset using some unique data from the row
being edited in the DataGrid (primary key) to locate the row of the dataset
that matches the row being edited in the DataGrid.

In addition, if you are using bound controls to show the row data in the
DataGrid, you can only grab those control values by asking for the control
by name. So, this code is an example of how to get the row in the DataSet
that matches the row in the DataGrid being edited:

Dim theRowToUpdate As DataRow = _
ds.Tables(0).Select("PartNumber='" & _
CType(e.Item.FindControl("lblPartNum"), Label).Text & "'")(0)



This example assumes you have a column in the DataSet named "PartNumber" and
a label in the DataGrid called "lblPartNum" that contains Primary Key
information about the record being edited. The "FindControl" method returns
an array, so at the end of the expression, I have "(0)" because I want the
first (and when using Primary Key data) and only row found.

You must also cast the control that is found into the correct type because
"FindControl" will only return objects and you'll want to extract the bound
data from the control via its bound property, in this case "Text".

As you can see, I am finding the row that contains a record with a primary
key value that matches the primary key data displayed in the grid. I am
then making a pointer to that row for easy access to it going forward.

Now, to update the rest of the data in the DataSet row, you'd need something
like this:

With theRowToUpdate
.Item("Name") = DirectCast(e.Item.FindControl("txtName"),
TextBox).Text
.Item("MFG") = DirectCast(e.Item.FindControl("txtMFG"), TextBox).Text
.Item("WholesalePrice") =
CType(DirectCast(e.Item.FindControl("txtWP"), TextBox).Text, Decimal)
.Item("RetailPrice") = CType(DirectCast(e.Item.FindControl("txtRP"),
TextBox).Text, Decimal)
.Item("OnHand") = CType(DirectCast(e.Item.FindControl("txtOnHand"),
TextBox).Text, Integer)
.Item("PicURL") = DirectCast(e.Item.FindControl("txtPicURL"),
TextBox).Text
.Item("Style") = DirectCast(e.Item.FindControl("txtStyle"),
TextBox).Text
.Item("Description") =
DirectCast(e.Item.FindControl("txtDescription"), TextBox).Text
End With

Now, after your DataSet is updated you need to call
DataAdapter.Update(dataSet) to get the original datasource updated and you
need to rebind your grid.

"st" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I populate a DataGrid programmatically from a DataSet that has its
> DataAdapter SQL built dynamically, i.e. the DataGrid contents, field
> names and field count is not fixed.
>
> I am at the point where I need to write code to update the bound
> DataSet contents to reflect DataGrid row edit (singular) and I am
> looking for a slick way to do this. Simplistically, something along the
> lines of ds(dgindex) = dg(dgindex).
>
> Is this possible? Would I need to ascertain ds field names and
> iterate/correspond columns in the DataGrid.
>
> I am working in C#.
>
> Many thanks,
>
> Simon
>



 
Reply With Quote
 
st
Guest
Posts: n/a
 
      15th Jun 2005
Hi Scott,

Thanks for the information.

As the source table for the DataGrid can change, I need a solution that
doesn't hard-code field names into the update.

Has anybody created a routine that will cope with this situation?

Thanks,

Simon

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      15th Jun 2005
The general procedure would still remain the same. You will have to come up
with a way of "dynamically" determining the grid values. Determining the
field names dynamically is a simple matter of looking at the dataset columns
collection and then the column name property of each column object.

"st" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Scott,
>
> Thanks for the information.
>
> As the source table for the DataGrid can change, I need a solution that
> doesn't hard-code field names into the update.
>
> Has anybody created a routine that will cope with this situation?
>
> Thanks,
>
> Simon
>



 
Reply With Quote
 
Val Mazur \(MVP\)
Guest
Posts: n/a
 
      15th Jun 2005
Hi,

If you are using SQL Server, then you could use CommandBuilder class that
will allow you automatically build INSERT, UPDATE and DELETE SQL statements
for your DataAdapter based on your SELECT statement. I believe it should
work for you

--
Val Mazur
Microsoft MVP

http://xport.mvps.org



"st" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Scott,
>
> Thanks for the information.
>
> As the source table for the DataGrid can change, I need a solution that
> doesn't hard-code field names into the update.
>
> Has anybody created a routine that will cope with this situation?
>
> Thanks,
>
> Simon
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      15th Jun 2005
Simon,

I don't think that I understand your problem completly.

However when you set the datasource to the datatable.defaultview than you
can forever find the row by using that defaultview (although it is a
datarowview).

Another method and probably for you the most easy one is using the
currencymanager, which gives you forever the current row that is in the
datagrid.

http://msdn.microsoft.com/library/de...classtopic.asp

When I look at your subject than the answer is just update the datatable
using the commandbuilder as Val already wrote. Don't forget to do an
endcurrentedit before you update.

http://msdn.microsoft.com/library/de...tedittopic.asp

I hope this helps,

Cor


 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      15th Jun 2005
How does this help the OP? The question wasn't how to dynamically generate
tables, it was how to dynamically update a dataset from changes in a grid
when the dataset has been created dynamically.

In addition, a CommandBuilder is also available for other data sources
besides SQL, however it is generally considered NOT a good idea to use it as
it is limited in situations where it will create correct statements.


"Val Mazur (MVP)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> If you are using SQL Server, then you could use CommandBuilder class that
> will allow you automatically build INSERT, UPDATE and DELETE SQL
> statements for your DataAdapter based on your SELECT statement. I believe
> it should work for you
>
> --
> Val Mazur
> Microsoft MVP
>
> http://xport.mvps.org
>
>
>
> "st" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi Scott,
>>
>> Thanks for the information.
>>
>> As the source table for the DataGrid can change, I need a solution that
>> doesn't hard-code field names into the update.
>>
>> Has anybody created a routine that will cope with this situation?
>>
>> Thanks,
>>
>> Simon
>>

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      15th Jun 2005
How does the DefaultView of the DataTable return the index in the DataSet
that corresponds to the row being edited in the DataGrid?


"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Simon,
>
> I don't think that I understand your problem completly.
>
> However when you set the datasource to the datatable.defaultview than you
> can forever find the row by using that defaultview (although it is a
> datarowview).
>
> Another method and probably for you the most easy one is using the
> currencymanager, which gives you forever the current row that is in the
> datagrid.
>
> http://msdn.microsoft.com/library/de...classtopic.asp
>
> When I look at your subject than the answer is just update the datatable
> using the commandbuilder as Val already wrote. Don't forget to do an
> endcurrentedit before you update.
>
> http://msdn.microsoft.com/library/de...tedittopic.asp
>
> I hope this helps,
>
> Cor
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bound Datagrid and dataset - update??? Darryn Microsoft ADO .NET 1 31st May 2005 10:17 PM
Update DataSet from Datagrid =?Utf-8?B?RVN0ZWlu?= Microsoft Dot NET 0 28th Mar 2005 02:09 AM
Re: DataGrid does not update DataSet it is bound to Homa Microsoft Dot NET Framework Forms 1 16th Dec 2004 09:02 PM
DataGrid Update after DataSet Change? Paul Brown Microsoft Dot NET Framework Forms 1 4th Nov 2003 11:04 PM
Ablity to Update DataSet from DataGrid ETuggle Microsoft VB .NET 1 6th Oct 2003 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:57 AM.