473,395 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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,@com ments)", 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>

Nov 19 '05 #1
2 1981
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.Cel ls(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.

Nov 19 '05 #2
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("resp onsible")
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...

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
by: jm | last post by:
I have datagrid. On this datagrid there is a button and a dropdownlist. When I press the button, I want the selectedIndex of the dropdownlist. There is a button and a dropdownlist for each...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
6
by: VB Programmer | last post by:
I have an itemtemplate in a datagrid. I'm trying to set it's value based on data. Having no trouble with the textbox. But, how do I do the same thing for a dropdownlist???? <ItemTemplate>...
3
by: Richard | last post by:
I've seen articles on GotDotNet and elsewhere on how to put a ddl in a datagrid, and have been able to implement this technique. For a new item, among the datagrid columns there is the one ddl for...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
4
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet,...
0
by: stevem2112 | last post by:
I have a datagrid with 2 Template columns. One column has DropDownLists and the other has Textboxes. I bind each DDL in the ItemCreated event. This datagrid is inside a UserControl that is inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.