Datagrid Dropdownlist in default in edit mode

J

jason

Hi.

The below code works, except, when I edit a record and update, the two
drop downs always take the first entry in the dropdownlist if not
selected. I'd also like the dropdown to show the currently set 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 threads in the
listgroup look right on, but are not clear to me.


Thank you 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=@swaiting­on,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>
 
S

Scott M.

This doesn't have to do with your question but your BindData method should
only be called one time, in the If Not IsPostBack section of your Page_Load.

By having it also called in your EditCommand and CancelCommand event
handlers (and other places), you are saying that when someone hits Cancel or
Edit, you want to open your connection and go back to the database and
re-query it!

Replace the call to your BindData method in the EditCommand and the
CancelCommand event handlers (and anywhere else you may have it) with:
UserGrid.DataBind()



Hi.

The below code works, except, when I edit a record and update, the two
drop downs always take the first entry in the dropdownlist if not
selected. I'd also like the dropdown to show the currently set 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 threads in the
listgroup look right on, but are not clear to me.


Thank you 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=@swaiting­on,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>
 
E

Elton Wang

Hi Jason,

Try following code in UserGrid_Edit(After BindData())


Dim ddlReason As DropDownList = UserGrid.Items
(e.Item.ItemIndex).FindControl("responsible")
Dim colIndex As Integer = 2
Dim reason As String = CType(e.Item.Cells
(colIndex).Controls(0), Label).Text.Trim
'if it doen't work try following
'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
ddl.SelectedIndex = I
Exit For
End If
Next

HTH,

Elton Wang
(e-mail address removed)
-----Original Message-----
Hi.

The below code works, except, when I edit a record and update, the two
drop downs always take the first entry in the dropdownlist if not
selected. I'd also like the dropdown to show the currently set 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 threads in the
listgroup look right on, but are not clear to me.


Thank you 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=@swaiti ng­on,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)
 
J

jason

Elton said:
Hi Jason,

Try following code in UserGrid_Edit(After BindData())


Dim ddlReason As DropDownList = UserGrid.Items
(e.Item.ItemIndex).FindControl("responsible")
Dim colIndex As Integer = 2
Dim reason As String = CType(e.Item.Cells
(colIndex).Controls(0), Label).Text.Trim
'if it doen't work try following
'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
ddl.SelectedIndex = I
Exit For
End If
Next

Hi Elton,

Thanks for you help.

I think you meant

ddlreason.selectedindex = I on that 4th to the last line right?

When I try this code with that fixed, I get:

Specified argument was out of the range of valid values. Parameter
name: index

Thanks still stuck on this...
 
J

jason

BTW - also trying this code, but's not working either...


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'm getting desperate - I'll paypal $20 to anybody whoever sends a
working solution first.

Thanks.
 
E

Elton Wang

Line 3
Dim colIndex As Integer = 2
Should be
Dim colIndex As Integer = 3

HTH

Elton Wang
 
J

jason

That was it!!! works awesome. THANK YOU SO MUCH!

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top