473,545 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

?? 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="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.OleDb" %>
<%@ import Namespace="Syst em.String" %>
<%@ import Namespace="Syst em.Web.Mail" %>
<script runat="server">
Sub Page_Load(sende r As Object, e As EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub
Public Sub BindData()
Dim objConn as new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=db.mdb")

objConn.Open()
Dim oaUser As OleDbDataAdapte r
Dim UserDS as DataSet = New DataSet()
oaUser = New OleDbDataAdapte r("Select * FROM appworx", objConn)
oaUser.Fill(Use rDS,"appworx")
objConn.Close
UserGrid.DataSo urce = UserDS.Tables(" appworx")
UserGrid.DataBi nd()
End Sub
Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommand EventArgs)
UserGrid.EditIt emIndex = E.Item.ItemInde x
BindData()
End Sub
Public Sub UserGrid_Cancel (Source As Object, E As
DataGridCommand EventArgs)
UserGrid.EditIt emIndex = -1
BindData()
End Sub
Public Sub UserGrid_Update (Source As Object, E As
DataGridCommand EventArgs)
Dim objConn as new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand ("UPDATE appworx SET
responsible=@re sponsible,statu s=@sstatus,wait ingon=@swaiting on,comments=@co mments
WHERE chain=@chain", objConn)

Dim schain As String = e.Item.Cells(2) .text
Dim sresponsible As String =
CType(e.Item.fi ndcontrol("resp onsible"),
Dropdownlist).S electedItem.val ue
Dim sstatus As String = CType(e.Item.fi ndcontrol("stat us"),
Dropdownlist).S electedItem.val ue
Dim swaitingon As String = CType(e.Item.Ce lls(5).Controls (0),
TextBox).Text
Dim scomments As String = CType(e.Item.Ce lls(6).Controls (0),
TextBox).Text

cmd.Parameters. Add(new OleDbParameter( "@responsib le",
sresponsible))
cmd.Parameters. Add(new OleDbParameter( "@status", sstatus))
cmd.Parameters. Add(new OleDbParameter( "@waitingon",sw aitingon))
cmd.Parameters. Add(new OleDbParameter ("@comments",sc omments))
cmd.Parameters. Add(new OleDbParameter( "@chain", schain))

objConn.Open()
cmd.ExecuteNonQ uery()
objConn.Close

UserGrid.EditIt emIndex = -1
BindData()

End Sub
Public Sub UserGrid_Comman d(sender As Object, e As
DataGridCommand EventArgs)
Select (CType(e.Comman dSource, LinkButton)).Co mmandName
Case "Delete"
Dim objConn as new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=db.mdb")

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

cmd.Parameters. Add(new OleDbParameter( "@chain",
e.Item.Cells(2) .Text))
objConn.Open()
cmd.ExecuteNonQ uery()
objConn.Close
Case Else
' Do Nothing
End Select
BindData()
End Sub
Public Sub UserGrid_ItemCr eated(sender As Object, e As
DataGridItemEve ntArgs)
Select Case e.Item.ItemType
Case ListItemType.It em, ListItemType.Al ternatingItem,
ListItemType.Ed itItem
' Add confirmation to Delete button
Dim tblCell As TableCell
Dim btnDelete As LinkButton
tblCell = e.Item.Cells(1)
btnDelete = tblCell.Control s(0)
btnDelete.Attri butes.Add("oncl ick", "return confirm('Are you
sure you want to delete?');")
End Select
End Sub
Public Sub newrec_Click(se nder As Object, e As EventArgs)

addrecord.visib le = false
addinsert.visib le = true
AddCancel.Visib le = true
addchain.Visibl e = true
addresponsible. Visible = true
addstatus.Visib le = true
addwaitingon.Vi sible = true
addcomments.Vis ible = true

End Sub
Public Sub Insert_Click(se nder As Object, e As EventArgs)
Dim objConn as new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=db.mdb")

Dim cmd As OleDbCommand = new OleDbCommand("I NSERT INTO appworx
(chain, responsible,sta tus,waitingon,c omments)
values(@chain,@ responsible,@st atus,@waitingon ,@comments)", objConn)
cmd.Parameters. Add(new OleDbParameter( "@chain", Addchain.Text))
cmd.Parameters. Add(new OleDbParameter( "@responsib le",
Addresponsible. Text))
cmd.Parameters. Add(new OleDbParameter( "@status", Addstatus.Text) )

cmd.Parameters. Add(new OleDbParameter( "@waitingon ",
Addwaitingon.Te xt))
cmd.Parameters. Add(new OleDbParameter( "@comments" ,
Addcomments.Tex t))
objConn.Open()
cmd.ExecuteNonQ uery()
objConn.Close
addrecord.visib le = true
addinsert.visib le = false
AddCancel.Visib le = false
addchain.Visibl e = false
addresponsible. Visible = false
addstatus.Visib le = false
addwaitingon.Vi sible = false
addcomments.Vis ible = false


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

addrecord.visib le = true
addinsert.visib le = false
addcancel.Visib le = false
addchain.Visibl e = false
addresponsible. Visible = false
addstatus.Visib le = false
addwaitingon.Vi sible = false
addcomments.Vis ible = false

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

End Sub
</script>
<html>
<head>
</head>
<body>
<form method="post" runat="server">
<table bgcolor=lightgr ey border=0>
<tr>
<td span=2 width=160px><as p:linkbutton id ="AddRecord"
width=40px Text="New" runat="server" visible="true"
onClick="NewRec _Click" /> </td>
<td width=70px>Chai n</td>
<td width=100px>Res ponsible</td>
<td width=100px>Sta tus</td>
<td width=100px>Wai ting On</td>
<td width=300px>Com ments</td>
</tr>
<tr>
<td span=2 width=160px>
<asp:linkbutt on id ="AddInsert" Text="Update" runat="server"
visible="false" onClick="Insert _Click" />
<asp:linkbutt on id="AddCancel" Text="Cancel" runat="server"
visible="false" onClick="AddCan cel_Click"/></td>
<td ><asp:textbox id="Addchain" width=70px runat="server"
visible="false" /></td>
<td><asp:textbo x id="Addresponsi ble" width=100px runat="server"
visible="false" /></td>
<td><asp:textbo x id="Addstatus" width=100px runat="server"
visible="false"/></td>
<td><asp:textbo x id="Addwaitingo n" width=100px runat="server"
visible="false" /></td>
<td><asp:textbo x id="Addcomments " width=300px runat="server"
visible="false" /></td>
</table>

<asp:datagrid id="UserGrid" runat=server AutoGenerateCol umns=false
BorderStyle="Do tted" BorderWidth="2"
BackgroundColor ="red"
CellPadding="5"
Font-Name="Arial" Font-Size="10pt"
OnEditCommand=" UserGrid_Edit"
OnCancelCommand ="UserGrid_Canc el"
OnUpdateCommand ="UserGrid_Upda te"
OnItemCommand=" UserGrid_Comman d"
OnItemCreated=" UserGrid_ItemCr eated">
<EditItemStyl e BackColor="yell ow">
</EditItemStyle>
<ItemStyle Wrap="false">
</ItemStyle>
<Columns>
<asp:EditComman dColumn
ButtonType ="LinkButton "
CancelText = "Cancel"
EditText = "Edit"
UpdateText = "Update"

<itemstyle width=100px />
</asp:EditCommand Column>

<asp:ButtonColu mn

ButtonType="Lin kButton"
Text="Delete"
CommandName="De lete"/>
<asp:BoundColum n DataField = "chain" ReadOnly = true >
<itemstyle width=70px />
</asp:BoundColumn >


<asp:TemplateCo lumn SortExpression= "responsibl e" >
<itemstyle width=100px />

<ItemTemplate >
<asp:Label Text='<%# Container.DataI tem("responsibl e") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>

<EditItemTempla te>
<asp:DropDownLi st 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:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn>
<itemstyle width=100px />
<ItemTemplate >
<asp:Label runat="server" Text='<%# Container.DataI tem("status")
%>' ID="Llbstat"/>

</ItemTemplate>

<EditItemTempla te>
<asp:DropDownLi st runat="server" id="status">
<asp:ListItem value="Pending" >Pending</asp:ListItem>
<asp:ListItem value="Developi ng">Developin g</asp:ListItem>
<asp:ListItem value="Testing" >Testing</asp:ListItem>
<asp:ListItem value="Hold">Ho ld</asp:ListItem>
<asp:ListItem value="Passed"> Passed</asp:ListItem>
<asp:ListItem value="Delete"> Delete</asp:ListItem>
</asp:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateCol umn>


<asp:BoundColum n DataField = "waitingon" >
<itemstyle width=100px />
</asp:BoundColumn >
<asp:BoundColum n DataField = "comments">
<itemstyle width=300px />
</asp:BoundColumn >

</Columns>
</asp:datagrid>
</form>

Nov 19 '05 #1
2 2001
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
DataGridCommand EventArgs)
UserGrid.EditIt emIndex = E.Item.ItemInde x
BindData()

Dim ddl As DropDownList = CType(e.Item.Fi ndControl("resp onsible"),
DropDownList)
ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(e.Ite m.Cells(3).Text ))
Ctype(e.Item.Ce lls(3).Controls (0), TextBox).Text =
CType(e.Item.Fi ndControl("resp onsible"),
DropDownList).S electedItem.Val ue()

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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error at line:

Line 40: ddl.SelectedInd ex = ddl.Items.Index Of(ddl.Items.Fi ndByValue

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
DataGridCommand EventArgs)
UserGrid.EditIt emIndex = E.Item.ItemInde x
BindData
()
Dim
ddlReason As DropDownList =
UserGrid.Items( e.Item.ItemInde x).FindControl( "responsibl e")
Dim colIndex As Integer
= 3
Dim

reason As String = CType(e.Item.Ce lls(colIndex).C ontrols(1),
Label).Text.Tri m
For I As Integer = 0 To ddlReason.Items .Count - 1
If ddlReason.Items (I).Text.Trim.E quals(reason) Then
ddlReason.Selec tedIndex = 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:TemplateCol umn SortExpression= "responsibl e" >
<itemstyle width=100px />
<ItemTemplate >
<asp:Label Text='<%# Container.DataI tem("responsibl e") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>
<EditItemTempla te>
<asp:DropDownLi st 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:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateCol umn>

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
2765
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 Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%#...
2
16983
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 the columns of the data grid contains a DropDownlist. I managed to create this datagrid control as follows.
2
3909
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 datagrid row that is populated from the database. Example, five rows returned, five buttons, and five dropdownlists. I know I am supposed to use...
18
2425
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 textbox, and in the second row I need to bind data into a dropdownlist...It all depends on the data I select from the database. I cannot use...
6
1764
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> <asp:DropDownList id="ddlYesNo" runat="server" > <asp:ListItem Value="-">-</asp:ListItem> <asp:ListItem Value="Yes">Yes</asp:ListItem> <asp:ListItem...
3
1800
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 the user to choose an account description, and when the user saves, then the value is saved and displayed in a bound column in the datagrid. So far...
0
1816
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 it's a Sharepoint WebPart. I'm trying to do something fairly simple, create a datagrid that displays where and when a person works and allows them to...
4
2004
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, when the datagrid enters it's own OnPreRender, the selected index has reverted to zero. I created a debug version of the dropdown list which...
0
1761
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 a user control (long story). Anyway, the user clicks a save button on the parent control to initiate the postback. The parent then calls a Save...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.