Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Multiple rows update

Author  Topic 

jhermiz

3564 Posts

Posted - 2006-02-23 : 15:19:06
The apps I have written so far deal with editing one item at a time in a datagrid. Making use of the EditItemIndex routine...

However, I need to be able to edit multiple rows in a datagrid (present the data grid in edit mode right away), and have one "UPDATE" button in the footer of the grid that loops through edited rows and updates the data.

I found some articles online but some are poorly written or hard to understand. Plus this data grid will have a combo box, and SOME of the rows should NOT be editable.

Does anyone have any VB.net with ASP.net samples of a datagrid that has a combo box that is updateable, and like 5 / 6 other columns that are not updateable, with one global UPDATE button?

I dont want to use the basic one row edit method that I have used in the past. I would really like to update multiple rows of a datagrid at a time.

Thanks,
Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]

jhermiz

3564 Posts

Posted - 2006-02-23 : 15:31:38
This is kind of what I want: http://www.superdotnet.com/Article.aspx?ArticleID=183

But the issue is I will have a combo box that the user should be able to update...
The combo box should be set to the current value of what is stored in the field from the db, but still list all of the other entries into the combo box that are valid values. When should I bind this combo box? Since the combo box will be on multiple rows of the data grid...make sense :) ?



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-23 : 16:20:11
Hi John -- define a templated column like this:

<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="DropList" Runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>


Then, on your datagrid, you can do somethign like this in the ItemDataBound event:


Private Sub dg_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dg.ItemDataBound

Dim d As DropDownList = CType(e.Item.FindControl("DropList"), DropDownList)

If Not d Is Nothing Then
d.DataSource = New String() {"a", "b", "c"}
d.DataBind()
End If
End Sub


In that example, I am just setting the datasource to the string array. You can bind it to what you need. You can also set the value as well during this event.

Let me know if this helps.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-23 : 16:52:24
Hmm ya I understood the ItemTemplate since Im dealing with a combo box, however,
let us say i have 20 rows in my grid. Will each row contain that combo box with its associated value from the db as well as the possible values. That was my main question.


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-23 : 16:56:56
quote:
Originally posted by jhermiz

Hmm ya I understood the ItemTemplate since Im dealing with a combo box, however,
let us say i have 20 rows in my grid. Will each row contain that combo box with its associated value from the db as well as the possible values. That was my main question.


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]



On the ItemDataBound() event, after finding the dropdownlist, you can set the value to whatever you need it to be.

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-23 : 16:59:49
For example:

Private Sub dg_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dg.ItemDataBound

Dim d As DropDownList = CType(e.Item.FindControl("DropList"), DropDownList)

If Not d Is Nothing Then
d.DataSource = {your datatable or datareader or array or whatever here}
d.SelectedValue = DataBinder.GetPropertyValue(e.Item.DataItem, "SomeColumnName").ToString()
d.DataBind()
End If

Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-23 : 17:03:59
hmm and this applies to all 20rows in the grid, each having that combo box right?
Im just having a stressful day...just bought a house...and I've been working on too many different tools so I'm just not seeing it :).

Thanks for your time, Ill check back tomorrow I've got to get out of here today.

Jon


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-23 : 18:48:31
The ItemDataBound event fires for each item that is created during the databinding process. A very useful event to trap.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-23 : 21:13:45
quote:
Originally posted by jsmith8858

The ItemDataBound event fires for each item that is created during the databinding process. A very useful event to trap.



Sorry Jeff,

Was at home just logged in.

So let us say I have a table of users with the following:

Jeff
Shannon
Jon
Steve
Allison

In each row there is that dropdownlist, say dl. There are a total of 4 returned rows. I would want all 4 rows to return all the data, plus the combo box. The combo box would have all those names but would be set to the value in the db. For simplicity, let us assume Jeff is in the first two rows and Shannon is in the other 2.

So in the ItemDataBound event I should call to load each combo box per row, plus set its value ?

Is that what you mean ? I am also assuming the other columns can just be labels since they are not editable.

Thanks,
Jon


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-23 : 21:28:17
>>So in the ItemDataBound event I should call to load each combo box per row, plus set its value ? Is that what you mean ?

Yes -- that's what the code I showed you is doing. Again, remember that event fires for *each* item created during the data binding stage. If your datagrid has 10 rows, this event fires 10 times. That's why we use e.Item.Findcontrol() in that event to find the combobox *within* the item that was created during the databinding event. Each reference returned during that event is a new dropdownlist. So, we need to fill it with values somehow, and then we need to set it.

>>I am also assuming the other columns can just be labels since they are not editable.

Yes. the other columns can be labels or text boxes or whatever you want -- I am only showing you the column for the dropdownlist column. If you need help with the others, let me know, but that link you provided seemed pretty straight forward.



Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-23 : 22:04:03
quote:
Originally posted by jsmith8858

>>So in the ItemDataBound event I should call to load each combo box per row, plus set its value ? Is that what you mean ?

Yes -- that's what the code I showed you is doing. Again, remember that event fires for *each* item created during the data binding stage. If your datagrid has 10 rows, this event fires 10 times. That's why we use e.Item.Findcontrol() in that event to find the combobox *within* the item that was created during the databinding event. Each reference returned during that event is a new dropdownlist. So, we need to fill it with values somehow, and then we need to set it.

>>I am also assuming the other columns can just be labels since they are not editable.

Yes. the other columns can be labels or text boxes or whatever you want -- I am only showing you the column for the dropdownlist column. If you need help with the others, let me know, but that link you provided seemed pretty straight forward.







cOOL you're still awake!

I'll work on this some more tomorrow. Ill post back if I run into any issues. Thanks for the heads up!


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-23 : 23:40:56
Jeff,

Would it be wise to add one additional like invisible field inside the datagrid such as a checkbox. That way if a change is made I can set that checkbox per the row to true. Then when my end user is done making updates he / she will click "Update" and then I can loop for only those checked rows?

I guess my question is this, the only thing that WILL be editable in a row is this combo box inside the datagrid. The rest of the fields are simply text boxes or labels. So does a combo box inside of a datagrid still contain the SelectedIndexChanged routine that a normal drop down list has ? That way if someone changes the data by selecting a different name I can set this "invisible" checkbox to true.

Make sense ?


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-24 : 08:43:05
they are regulard dropDownLists -- there is nothning special or different about them because they are in a datagrid.

I don't think you want a postback everytime one of them changes, do you?

I would just put a hidden field on item row right next to the combobox, and store the initial value of the frield from the database there. Then, when it is time to update the DB, you loop through and compare the combobox values with the initial value for each, and perform updates to the DB as necessary. *WARNING* -- this will not work well if you might have multiple users potentially editing this list of checkboxes all at once.

I would start trying this out -- in test area, of course, with northwind or something, keep it small and simple -- before asking too many more questions.

Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-24 : 11:57:32
Jeff,

Wanted to let you know I got the majority of the grid working thanks especially to you!
The final thing I am working on is the "Update" or "Save" button. Can you post some relevant code, in addition, basically the for loop to go through each row. I did add a hidden label field to store the Container.DataItem CONTACT field (this is sitting next to the combo box and will be used to perform the comparison that you had hinted me on). So I just need to compare the two, and make a request to the db (parameters). If you don't mind can you post some relevant code that coudl help me loop, compare, and then call my sproc?

Thanks again, very solid and helpful hints man.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-24 : 13:08:53
Nevermind the rest jeff, I got it all working. Thanks for the initial heads up.

Jon


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page
   

- Advertisement -