DataGrid Question

G

Guest

I have a Datagrid that I can't seem to get to work. I can populate the grid
from a database but I can't figure out how to update the DB when the user
edits a templated column. I have done it before and copied the code over but
it isn't working. Any help is greatly appreciated.

Code behind:

Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As
DataGridCommandEventArgs)

'get a reference to the title and publication date text boxes
Dim objType As DropDownList
objType = CType(objArgs.Item.FindControl("UsrStatus"), DropDownList)

'OBJTYPE.SELECTEDVALUE IS 0 NO MATTER WHAT SELECTION IS MADE
Response.Write(objType.SelectedValue & " - " &
UnitLists.DataKeys(objArgs.Item.ItemIndex))

'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE Unit_General SET Status=" &
CInt(objType.SelectedValue) & " WHERE ID=" &
UnitLists.DataKeys(objArgs.Item.ItemIndex)
Response.Write(strSQL)
ExecuteSQLStatement(strSQL)

If objType.SelectedValue = 0 Then
strSQL = "DELETE * FROM Users_Units WHERE PID=" &
objType.SelectedValue
ExecuteSQLStatement(strSQL)
End If

'set EditItemIndex property of grid to -1 to switch out of Edit mode
UnitLists.EditItemIndex = -1
BindDataGrid() 'bind the data and display it

End Sub

HTML PAGE:
<form id="Form1" method="post" runat="server">
<asp:datagrid id="UnitLists" runat="server" AllowSorting="True"
PageSize="50" EditItemStyle-BackColor="yellow"
AutoGenerateColumns="False" DataKeyField="ID" Width="696px"
BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Horizontal" ForeColor="Black"
OnCancelCommand="DoItemCancel" OnUpdateCommand="DoItemUpdate"
OnEditCommand="DoItemEdit">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CC3333"></SelectedItemStyle>
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#333333"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="UnitNumber" ReadOnly="True"
HeaderText="Unit #"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id="txtType" Runat="server" text='<%#
databinder.eval(Container.dataitem,"StatusText") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="UsrStatus" runat="server">
<asp:ListItem Value="0">Available</asp:ListItem>
<asp:ListItem Value="1">Pending</asp:ListItem>
<asp:ListItem Value="2">Unavailable</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="FNAME" ReadOnly=True
HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LNAME" ReadOnly=True
HeaderText="Last Name"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="White"></PagerStyle>
</asp:datagrid></form>
 
G

Guest

There are no errors - the data value that is being passed is always 0 even
though the drop down item's data value is 1 or 2.
 
E

Elton W

Hi Ryan,

The most likely reason is before executing DoItemUpdate
you run BindDataGrid(), e.g. in Page_Load. Hence data
changed by user is overwritten data source. The correct
approach is to only bind datagrid with data source when
it's not postback:

If Not IsPostback Then
Datagrid.DataSource = data_source_object
Datagrid.DataBind()
End If

HTH

Elton Wang
(e-mail address removed)


-----Original Message-----
I have a Datagrid that I can't seem to get to work. I can populate the grid
from a database but I can't figure out how to update the DB when the user
edits a templated column. I have done it before and copied the code over but
it isn't working. Any help is greatly appreciated.

Code behind:

Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As
DataGridCommandEventArgs)

'get a reference to the title and publication date text boxes
Dim objType As DropDownList
objType = CType(objArgs.Item.FindControl ("UsrStatus"), DropDownList)

'OBJTYPE.SELECTEDVALUE IS 0 NO MATTER WHAT SELECTION IS MADE
Response.Write(objType.SelectedValue & " - " &
UnitLists.DataKeys(objArgs.Item.ItemIndex))

'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE Unit_General SET Status=" &
CInt(objType.SelectedValue) & " WHERE ID=" &
UnitLists.DataKeys(objArgs.Item.ItemIndex)
Response.Write(strSQL)
ExecuteSQLStatement(strSQL)

If objType.SelectedValue = 0 Then
strSQL = "DELETE * FROM Users_Units WHERE PID=" &
objType.SelectedValue
ExecuteSQLStatement(strSQL)
End If

'set EditItemIndex property of grid to -1 to switch out of Edit mode
UnitLists.EditItemIndex = -1
BindDataGrid() 'bind the data and display it

End Sub

HTML PAGE:
<form id="Form1" method="post"
runat="server"><asp:datagrid id="UnitLists"
runat="server" AllowSorting="True"
PageSize="50" EditItemStyle-BackColor="yellow"

AutoGenerateColumns="False" DataKeyField="ID"
Width="696px"
BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px"
BackColor="White" CellPadding="4"
GridLines="Horizontal" ForeColor="Black"

OnCancelCommand="DoItemCancel"
OnUpdateCommand="DoItemUpdate"
OnEditCommand="DoItemEdit">
<SelectedItemStyle Font-
Bold="True" ForeColor="White"
BackColor="#CC3333"></SelectedItemStyle>
<EditItemStyle
<AlternatingItemStyle
<HeaderStyle Font-
Bold="True" ForeColor="White"
BackColor="#333333"></HeaderStyle>
<FooterStyle
<asp:BoundColumn
DataField="UnitNumber" ReadOnly="True"
HeaderText="Unit #"></asp:BoundColumn>



<asp:Label id="txtType" Runat="server" text='<%#
databinder.eval(Container.dataitem,"StatusText") %>'>










<asp:BoundColumn
DataField="FNAME" ReadOnly=True
HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn
DataField="LNAME" ReadOnly=True
HeaderText="Last Name"></asp:BoundColumn>

<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<PagerStyle
HorizontalAlign="Right" ForeColor="Black"
 

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,985
Messages
2,570,202
Members
46,777
Latest member
JosefinaAc

Latest Threads

Top