?? dropdownlist in datagrid, not defaulting to current value ??? THANK YOU

J

jason

Pardon my ignorance on this. The below code works, except, when I edit
a record and update the two drop downs take the first entry in the
dropdownlist if not selected. I'd also like the dropdown to show the
current value in edit mode. I'm sure this is a common question. I've
reviewed several related post and tried them out to no avial. Some of
the offerings in listgroup look right on, but are not clear on where to
handle the suggested events.


Thanks for any help or information :


<%@ Page Language="VB" Debug=true%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.String" %>
<%@ import Namespace="System.Web.Mail" %>


<script runat="server">


Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub


Public Sub BindData()
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")

objConn.Open()
Dim oaUser As OleDbDataAdapter
Dim UserDS as DataSet = New DataSet()
oaUser = New OleDbDataAdapter("Select * FROM appworx", objConn)


oaUser.Fill(UserDS,"appworx")
objConn.Close


UserGrid.DataSource = UserDS.Tables("appworx")
UserGrid.DataBind()
End Sub


Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData()
End Sub


Public Sub UserGrid_Cancel (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = -1
BindData()
End Sub


Public Sub UserGrid_Update (Source As Object, E As
DataGridCommandEventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")


Dim cmd As OleDbCommand = new OleDbCommand ("UPDATE appworx SET
responsible=@responsible,status=@sstatus,waitingon=@swaitingon,comments=@comments
WHERE chain=@chain", objConn)

Dim schain As String = e.Item.Cells(2).text
Dim sresponsible As String =
CType(e.Item.findcontrol("responsible"),
Dropdownlist).SelectedItem.value
Dim sstatus As String = CType(e.Item.findcontrol("status"),
Dropdownlist).SelectedItem.value
Dim swaitingon As String = CType(e.Item.Cells(5).Controls (0),
TextBox).Text
Dim scomments As String = CType(e.Item.Cells(6).Controls (0),
TextBox).Text



cmd.Parameters.Add(new OleDbParameter("@responsible",
sresponsible))
cmd.Parameters.Add(new OleDbParameter("@status", sstatus))
cmd.Parameters.Add(new OleDbParameter("@waitingon",swaitingon))
cmd.Parameters.Add(new OleDbParameter ("@comments",scomments))
cmd.Parameters.Add(new OleDbParameter("@chain", schain))



objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close

UserGrid.EditItemIndex = -1
BindData()



End Sub


Public Sub UserGrid_Command(sender As Object, e As
DataGridCommandEventArgs)
Select (CType(e.CommandSource, LinkButton)).CommandName


Case "Delete"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")

Dim cmd As OleDbCommand = new OleDbCommand("DELETE FROM
appworx WHERE chain = @chain", objConn)

cmd.Parameters.Add(new OleDbParameter("@chain",
e.Item.Cells(2).Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
Case Else
' Do Nothing


End Select


BindData()
End Sub


Public Sub UserGrid_ItemCreated(sender As Object, e As
DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem


' Add confirmation to Delete button
Dim tblCell As TableCell
Dim btnDelete As LinkButton


tblCell = e.Item.Cells(1)
btnDelete = tblCell.Controls(0)
btnDelete.Attributes.Add("onclick", "return confirm('Are you
sure you want to delete?');")
End Select
End Sub


Public Sub newrec_Click(sender As Object, e As EventArgs)

addrecord.visible = false
addinsert.visible = true
AddCancel.Visible = true
addchain.Visible = true
addresponsible.Visible = true
addstatus.Visible = true
addwaitingon.Visible = true
addcomments.Visible = true

End Sub


Public Sub Insert_Click(sender As Object, e As EventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")

Dim cmd As OleDbCommand = new OleDbCommand("INSERT INTO appworx
(chain, responsible,status,waitingon,comments)
values(@chain,@responsible,@status,@waitingon,@comments)", objConn)


cmd.Parameters.Add(new OleDbParameter("@chain", Addchain.Text))
cmd.Parameters.Add(new OleDbParameter("@responsible",
Addresponsible.Text))
cmd.Parameters.Add(new OleDbParameter("@status", Addstatus.Text))

cmd.Parameters.Add(new OleDbParameter("@waitingon",
Addwaitingon.Text))
cmd.Parameters.Add(new OleDbParameter("@comments",
Addcomments.Text))


objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close


addrecord.visible = true
addinsert.visible = false
AddCancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false




BindData()
End Sub


Public Sub AddCancel_Click(sender As Object, e As EventArgs)

addrecord.visible = true
addinsert.visible = false
addcancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false



' Reset text fields (for next time)
addchain.text = ""
addresponsible.text = ""
addstatus.text = ""
addwaitingon.text = ""
addcomments.text = ""

End Sub


</script>


<html>
<head>
</head>
<body>
<form method="post" runat="server">


<table bgcolor=lightgrey border=0>
<tr>
<td span=2 width=160px><asp:linkbutton id ="AddRecord"
width=40px Text="New" runat="server" visible="true"
onClick="NewRec_Click" /> </td>
<td width=70px>Chain</td>
<td width=100px>Responsible</td>
<td width=100px>Status</td>
<td width=100px>Waiting On</td>
<td width=300px>Comments</td>


</tr>
<tr>
<td span=2 width=160px>
<asp:linkbutton id ="AddInsert" Text="Update" runat="server"
visible="false" onClick="Insert_Click" />
<asp:linkbutton id="AddCancel" Text="Cancel" runat="server"
visible="false" onClick="AddCancel_Click"/></td>
<td ><asp:textbox id="Addchain" width=70px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addresponsible" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addstatus" width=100px runat="server"
visible="false"/></td>
<td><asp:textbox id="Addwaitingon" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addcomments" width=300px runat="server"
visible="false" /></td>
</table>

<asp:datagrid id="UserGrid" runat=server AutoGenerateColumns=false
BorderStyle="Dotted" BorderWidth="2"
BackgroundColor="red"
CellPadding="5"
Font-Name="Arial" Font-Size="10pt"
OnEditCommand="UserGrid_Edit"
OnCancelCommand="UserGrid_Cancel"
OnUpdateCommand="UserGrid_Update"
OnItemCommand="UserGrid_Command"
OnItemCreated="UserGrid_ItemCreated">


<EditItemStyle BackColor="yellow">
</EditItemStyle>


<ItemStyle Wrap="false">
</ItemStyle>


<Columns>
<asp:EditCommandColumn
ButtonType ="LinkButton"
CancelText = "Cancel"
EditText = "Edit"
UpdateText = "Update"<itemstyle width=100px />
</asp:EditCommandColumn>

<asp:ButtonColumn

ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>


<asp:BoundColumn DataField = "chain" ReadOnly = true >
<itemstyle width=70px />
</asp:BoundColumn>




<asp:TemplateColumn SortExpression="responsible" >
<itemstyle width=100px />

<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("responsible") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>

<EditItemTemplate>
<asp:DropDownList runat="server" id="responsible">
<asp:ListItem value="x">x</asp:ListItem>
<asp:ListItem value="y">y</asp:ListItem>
<asp:ListItem value="z">z</asp:ListItem>
<asp:ListItem value="g">g</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn>
<itemstyle width=100px />
<ItemTemplate >
<asp:Label runat="server" Text='<%# Container.DataItem("status")
%>' ID="Llbstat"/>

</ItemTemplate>

<EditItemTemplate>
<asp:DropDownList runat="server" id="status">
<asp:ListItem value="Pending">Pending</asp:ListItem>
<asp:ListItem value="Developing">Developing</asp:ListItem>
<asp:ListItem value="Testing">Testing</asp:ListItem>
<asp:ListItem value="Hold">Hold</asp:ListItem>
<asp:ListItem value="Passed">Passed</asp:ListItem>
<asp:ListItem value="Delete">Delete</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>




<asp:BoundColumn DataField = "waitingon">
<itemstyle width=100px />
</asp:BoundColumn>


<asp:BoundColumn DataField = "comments">
<itemstyle width=300px />
</asp:BoundColumn>



</Columns>
</asp:datagrid>


</form>
 
J

jason

Trying the below code, but's it's NOT working ...

The goal again being to show the current value of the dropdown in edit
mode for the datagrid.


Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData()

Dim ddl As DropDownList = CType(e.Item.FindControl("responsible"),
DropDownList)
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(e.Item.Cells(3).Text))
Ctype(e.Item.Cells(3).Controls(0), TextBox).Text =
CType(e.Item.FindControl("responsible"),
DropDownList).SelectedItem.Value()

End Sub



Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error at line:

Line 40: ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue

I'll paypal for a working solution at this point.

Thanks.
 
J

jason

Below is the solution - provide by another list server.

here's final code to display the current value of dropdownlist inside a

datagrid when in edit mode:



Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData
()
Dim
ddlReason As DropDownList =
UserGrid.Items(e.Item.ItemIndex).FindControl("responsible")
Dim colIndex As Integer


= 3
Dim

reason As String = CType(e.Item.Cells(colIndex).Controls(1),
Label).Text.Trim
For I As Integer = 0 To ddlReason.Items.Count - 1
If ddlReason.Items(I).Text.Trim.Equals(reason) Then


ddlReason.SelectedIndex = I
Exit For
End If
Next
End Sub

3 because it's the third column in my grid i suppose.
And here's the dropdownlist code inside my datagrid:


<



asp:TemplateColumn SortExpression="responsible" >
<itemstyle width=100px />
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("responsible") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="responsible">
<asp:ListItem value="x">x</asp:ListItem>
<asp:ListItem value="y">y</asp:ListItem>
<asp:ListItem value="z">z</asp:ListItem>
<asp:ListItem value


="z">z</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Maybe this will help others...
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top