473,769 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you load a dropdownlist when edit is clicked in a datagrid

How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
Jul 21 '05 #1
12 2805
Hi,
There are many ways to do it. One of them is to handle the ItemDataBound
event of the grid as given below:

if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList ddl = (DropDownList) e.Item.FindCont rol ("deViews");
// Build a dataset or data table with data to populate the drop-down
ddl.DataSource = <datatable name>;
ddl.DataTextFie ld = <datatable col name>;
ddl.DataValueFi eld = <datatable col name>;
ddl.DataBind();
}

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:8E******** *************** ***********@mic rosoft.com...
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>

Jul 21 '05 #2
I am trying to load a droplist in VB when the edit is clicked in a datagrid.
I tried to use OnDataBinding and loading the droplist in subroutine "loaddd".

I get this error Object reference not set to an instance of an object.

Here is the code:

<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" OnDataBinding=" loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArg s)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub

"Shiva" wrote:
Hi,
There are many ways to do it. One of them is to handle the ItemDataBound
event of the grid as given below:

if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList ddl = (DropDownList) e.Item.FindCont rol ("deViews");
// Build a dataset or data table with data to populate the drop-down
ddl.DataSource = <datatable name>;
ddl.DataTextFie ld = <datatable col name>;
ddl.DataValueFi eld = <datatable col name>;
ddl.DataBind();
}

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:8E******** *************** ***********@mic rosoft.com...
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>

Jul 21 '05 #3
Try this one:

.... ...
<EditItemTempla te>
<asp:dropdownli st id="deViews" runat="server"> </asp:dropdownlis t>
.... ....
</EditItemTemplat e>
.... ...

Assuming the datagrid id is dgrd1,

Sub dgrd1_ItemDataB ound (sender As Object, e As DataGridItemEve ntArgs)
Handles dgrd1.ItemDataB ound
If (e.Item.ItemTyp e = ListItemType.Ed itItem) Then
DropDownList ddl = CType (e.Item.FindCon trol ("deViews"), DropDownList)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New SqlCommand("Get _All_TypeofOpti ons",
myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)
End While

ddl .DataSource = dtrControlOptio n
ddl .DataTextField = "TypeOption "
ddl .DataValueField = "IdTypeOpti on"
ddl .DataBind()

myConnection.Cl ose()
'BindOption()
Dim i As Integer
For i = 0 To ddl.Items.Count - 1
Dim opchk As String = ddl.Items(i).Va lue
If opchk = Session("typeof option") Then
ddl.Items(i).Se lected = True
End If
Next
End If
End Sub

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:CF******** *************** ***********@mic rosoft.com...
I am trying to load a droplist in VB when the edit is clicked in a datagrid.
I tried to use OnDataBinding and loading the droplist in subroutine
"loaddd".

I get this error Object reference not set to an instance of an object.

Here is the code:

<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" OnDataBinding=" loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArg s)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub

"Shiva" wrote:
Hi,
There are many ways to do it. One of them is to handle the ItemDataBound
event of the grid as given below:

if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList ddl = (DropDownList) e.Item.FindCont rol ("deViews");
// Build a dataset or data table with data to populate the drop-down
ddl.DataSource = <datatable name>;
ddl.DataTextFie ld = <datatable col name>;
ddl.DataValueFi eld = <datatable col name>;
ddl.DataBind();
}

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:8E******** *************** ***********@mic rosoft.com...
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>

Jul 21 '05 #4
I still get this error. Object reference not set to an instance of an object.

Droplist deViews is it the EditItemTemplat e
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:
Line 211: End While
Line 212:
Line 213: c.DataSource = dtrControlOptio n
Line 214: deViews.DataTex tField = "TypeOption "
Line 215: deViews.DataVal ueField = "IdTypeOpti on"
Source File: C:\best\_Option Maint.ascx.vb Line: 213


<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False
Text='<%# DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As DataGridItemEve ntArgs)
Handles OptionGrid.Item DataBound

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub
"Shiva" wrote:
Try this one:

.... ...
<EditItemTempla te>
<asp:dropdownli st id="deViews" runat="server"> </asp:dropdownlis t>
.... ....
</EditItemTemplat e>
.... ...

Assuming the datagrid id is dgrd1,

Sub dgrd1_ItemDataB ound (sender As Object, e As DataGridItemEve ntArgs)
Handles dgrd1.ItemDataB ound
If (e.Item.ItemTyp e = ListItemType.Ed itItem) Then
DropDownList ddl = CType (e.Item.FindCon trol ("deViews"), DropDownList)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New SqlCommand("Get _All_TypeofOpti ons",
myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)
End While

ddl .DataSource = dtrControlOptio n
ddl .DataTextField = "TypeOption "
ddl .DataValueField = "IdTypeOpti on"
ddl .DataBind()

myConnection.Cl ose()
'BindOption()
Dim i As Integer
For i = 0 To ddl.Items.Count - 1
Dim opchk As String = ddl.Items(i).Va lue
If opchk = Session("typeof option") Then
ddl.Items(i).Se lected = True
End If
Next
End If
End Sub

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:CF******** *************** ***********@mic rosoft.com...
I am trying to load a droplist in VB when the edit is clicked in a datagrid.
I tried to use OnDataBinding and loading the droplist in subroutine
"loaddd".

I get this error Object reference not set to an instance of an object.

Here is the code:

<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" OnDataBinding=" loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArg s)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub

"Shiva" wrote:
Hi,
There are many ways to do it. One of them is to handle the ItemDataBound
event of the grid as given below:

if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList ddl = (DropDownList) e.Item.FindCont rol ("deViews");
// Build a dataset or data table with data to populate the drop-down
ddl.DataSource = <datatable name>;
ddl.DataTextFie ld = <datatable col name>;
ddl.DataValueFi eld = <datatable col name>;
ddl.DataBind();
}

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:8E******** *************** ***********@mic rosoft.com...
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>


Jul 21 '05 #5
Hi,

Your ItemDataBound code is still using the dropdown list id that was given
in the EditItemTemplat e. However, this id cannot be used as is. You have to
find the dropdownlist control instance in the current daragrid row and use
that for further usage. If you look at the code in my reply, there is a line
like this:

DropDownList ddl = CType (e.Item.FindCon trol ("deViews"), DropDownList)

Use ddl wherever you are using deView.

Also, the whole dropdown list populating code should run only if the current
datagrid row is of EditItem type. So, you need this line as well:

If (e.Item.ItemTyp e = ListItemType.Ed itItem) Then
'Populate the dropdown...
End If

HTH.

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:63******** *************** ***********@mic rosoft.com...
I still get this error. Object reference not set to an instance of an
object.

Droplist deViews is it the EditItemTemplat e
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:
Line 211: End While
Line 212:
Line 213: c.DataSource = dtrControlOptio n
Line 214: deViews.DataTex tField = "TypeOption "
Line 215: deViews.DataVal ueField = "IdTypeOpti on"
Source File: C:\best\_Option Maint.ascx.vb Line: 213


<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False
Text='<%# DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As DataGridItemEve ntArgs)
Handles OptionGrid.Item DataBound

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" )
dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub
"Shiva" wrote:
Try this one:

.... ...
<EditItemTempla te>
<asp:dropdownli st id="deViews" runat="server"> </asp:dropdownlis t>
.... ....
</EditItemTemplat e>
.... ...

Assuming the datagrid id is dgrd1,

Sub dgrd1_ItemDataB ound (sender As Object, e As DataGridItemEve ntArgs)
Handles dgrd1.ItemDataB ound
If (e.Item.ItemTyp e = ListItemType.Ed itItem) Then
DropDownList ddl = CType (e.Item.FindCon trol ("deViews"), DropDownList)
Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New SqlCommand("Get _All_TypeofOpti ons",
myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" ) dtrControlOptio n.Rows.Add(drop RowOption)
End While

ddl .DataSource = dtrControlOptio n
ddl .DataTextField = "TypeOption "
ddl .DataValueField = "IdTypeOpti on"
ddl .DataBind()

myConnection.Cl ose()
'BindOption()
Dim i As Integer
For i = 0 To ddl.Items.Count - 1
Dim opchk As String = ddl.Items(i).Va lue
If opchk = Session("typeof option") Then
ddl.Items(i).Se lected = True
End If
Next
End If
End Sub

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:CF******** *************** ***********@mic rosoft.com...
I am trying to load a droplist in VB when the edit is clicked in a datagrid. I tried to use OnDataBinding and loading the droplist in subroutine
"loaddd".

I get this error Object reference not set to an instance of an object.

Here is the code:

<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option"
SortExpression= "IdTypeOpti on">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" OnDataBinding=" loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" ID="IdTypeOptio n" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>
</asp:TemplateCol umn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArg s)

Dim myConnection As SqlConnection = New
SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As SqlCommand = New
SqlCommand("Get _All_TypeofOpti ons", myConnection)

Dim dtrControlOptio n As New DataTable
Dim dropRowOption As DataRow

dtrControlOptio n.Columns.Add( _
New DataColumn("Typ eOption", GetType(String) ))

dtrControlOptio n.Columns.Add( _
New DataColumn("IdT ypeOption", GetType(String) ))

myConnection.Op en()

Dim Optionfile As SqlDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)

While Optionfile.Read ()
dropRowOption = dtrControlOptio n.NewRow()
dropRowOption(" TypeOption") = Optionfile.Item ("TypeOption ")
dropRowOption(" IdTypeOption") = Optionfile.Item ("IdTypeOption" ) dtrControlOptio n.Rows.Add(drop RowOption)

End While

deViews.DataSou rce = dtrControlOptio n
deViews.DataTex tField = "TypeOption "
deViews.DataVal ueField = "IdTypeOpti on"
deViews.DataBin d()

myConnection.Cl ose()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.C ount - 1
Dim opchk As String = deViews.Items(i ).Value
Dim chk = deViews.DataVal ueField
If opchk = Session("typeof option") Then
deViews.Items(i ).Selected = True
End If
Next

End Sub

"Shiva" wrote:
Hi,
There are many ways to do it. One of them is to handle the ItemDataBound
event of the grid as given below:

if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList ddl = (DropDownList) e.Item.FindCont rol ("deViews");
// Build a dataset or data table with data to populate the drop-down
ddl.DataSource = <datatable name>;
ddl.DataTextFie ld = <datatable col name>;
ddl.DataValueFi eld = <datatable col name>;
ddl.DataBind();
}

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:8E******** *************** ***********@mic rosoft.com...
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>


Jul 21 '05 #6
I can not get the drowdownlist loaded. How do i do that?

"Stanley J Mroczek" wrote:
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>

Jul 21 '05 #7
Hi,
Check these out. They have samples:
http://msdn.microsoft.com/library/de...tomcolumns.asp

http://www.4guysfromrolla.com/webtech/050801-1.shtml

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:7F******** *************** ***********@mic rosoft.com...
I can not get the drowdownlist loaded. How do i do that?

"Stanley J Mroczek" wrote:
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>

Jul 21 '05 #8

That works find!! THANK YOU

!! Now my problem is when the dropdownlist ddlMan.DataSour ce =
Yearfile.Getall Manufacturer is changed

AutoPostBack=Tr ue OnSelectedIndex Changed= "newManufacture r"

How do get what was selected in (ddlMan)??

Sub newManufacturer (ByVal sender As Object, ByVal e As System.EventArg s)
Dim txtlbIIDManufac turer As Label
??????
End Sub
Private Sub loaddd(ByVal sender As System.Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles CarGrid.ItemDat aBound

If e.Item.ItemType = ListItemType.Ed itItem Then

Dim Yearfile As MaintenanceDB = New MaintenanceDB
Dim drv As DataRowView = CType(e.Item.Da taItem, DataRowView)
Dim currentYear As String = drv("IDCar")
Dim ddlyr As DropDownList = CType(e.Item.Ce lls(0).Controls (1),
DropDownList)
Dim ddlMan As DropDownList = CType(e.Item.Ce lls(1).Controls (1),
DropDownList)
Dim ddlModel As DropDownList =
CType(e.Item.Ce lls(2).Controls (1), DropDownList)

ddlyr.DataSourc e = Yearfile.GetAll Year
ddlyr.DataTextF ield = "YearDate"
ddlyr.DataValue Field = "IDYear"
ddlyr.DataBind( )

Dim i As Integer
For i = 0 To ddlyr.Items.Cou nt - 1
Dim opchk As String = ddlyr.Items(i). Value
Dim chk = ddlyr.DataValue Field
If opchk = Session("IdYear ") Then
ddlyr.Items(i). Selected = True
End If
Next

ddlMan.DataSour ce = Yearfile.Getall Manufacturer
ddlMan.DataText Field = "Manufactur er"
ddlMan.DataValu eField = "IDManufacturer "
ddlMan.DataBind ()

For i = 0 To ddlMan.Items.Co unt - 1
Dim opchk As String = ddlMan.Items(i) .Value
Dim chk = ddlMan.DataValu eField
If opchk = Session("IdIDMa nufacturer") Then
ddlMan.Items(i) .Selected = True
End If
Next


"Shiva" wrote:
Hi,
Check these out. They have samples:
http://msdn.microsoft.com/library/de...tomcolumns.asp

http://www.4guysfromrolla.com/webtech/050801-1.shtml

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:7F******** *************** ***********@mic rosoft.com...
I can not get the drowdownlist loaded. How do i do that?

"Stanley J Mroczek" wrote:
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>


Jul 21 '05 #9
This code gets the dropdownlist and from that you can get the selected
index. Have this in your newManufacturer () method:

Dim ddl As DropDownList
Dim item As DataGridItem = CarGrid.Items(C arGrid.EditItem Index)
If (Not item Is Nothing) Then
ddl = CType (item.FindContr ol ("deViews"), DropDownList)
'Now, refer ddl.SelectedIte m to get the selected item
End If

HTH

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:1C******** *************** ***********@mic rosoft.com...

That works find!! THANK YOU

!! Now my problem is when the dropdownlist ddlMan.DataSour ce =
Yearfile.Getall Manufacturer is changed

AutoPostBack=Tr ue OnSelectedIndex Changed= "newManufacture r"

How do get what was selected in (ddlMan)??

Sub newManufacturer (ByVal sender As Object, ByVal e As System.EventArg s)
Dim txtlbIIDManufac turer As Label
??????
End Sub
Private Sub loaddd(ByVal sender As System.Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
CarGrid.ItemDat aBound

If e.Item.ItemType = ListItemType.Ed itItem Then

Dim Yearfile As MaintenanceDB = New MaintenanceDB
Dim drv As DataRowView = CType(e.Item.Da taItem, DataRowView)
Dim currentYear As String = drv("IDCar")
Dim ddlyr As DropDownList = CType(e.Item.Ce lls(0).Controls (1),
DropDownList)
Dim ddlMan As DropDownList = CType(e.Item.Ce lls(1).Controls (1),
DropDownList)
Dim ddlModel As DropDownList =
CType(e.Item.Ce lls(2).Controls (1), DropDownList)

ddlyr.DataSourc e = Yearfile.GetAll Year
ddlyr.DataTextF ield = "YearDate"
ddlyr.DataValue Field = "IDYear"
ddlyr.DataBind( )

Dim i As Integer
For i = 0 To ddlyr.Items.Cou nt - 1
Dim opchk As String = ddlyr.Items(i). Value
Dim chk = ddlyr.DataValue Field
If opchk = Session("IdYear ") Then
ddlyr.Items(i). Selected = True
End If
Next

ddlMan.DataSour ce = Yearfile.Getall Manufacturer
ddlMan.DataText Field = "Manufactur er"
ddlMan.DataValu eField = "IDManufacturer "
ddlMan.DataBind ()

For i = 0 To ddlMan.Items.Co unt - 1
Dim opchk As String = ddlMan.Items(i) .Value
Dim chk = ddlMan.DataValu eField
If opchk = Session("IdIDMa nufacturer") Then
ddlMan.Items(i) .Selected = True
End If
Next


"Shiva" wrote:
Hi,
Check these out. They have samples:
http://msdn.microsoft.com/library/de...tomcolumns.asp
http://www.4guysfromrolla.com/webtech/050801-1.shtml

"Stanley J Mroczek" <St************ *@discussions.m icrosoft.com> wrote in
message news:7F******** *************** ***********@mic rosoft.com...
I can not get the drowdownlist loaded. How do i do that?

"Stanley J Mroczek" wrote:
How do you load a dropdownlist when edit is clicked in a datagrid ?

<Columns>
<asp:BoundColum n DataField="Opti onDescription" ItemStyle-Wrap="True"
HeaderText="Opt ion Description"></asp:BoundColumn >
<asp:TemplateCo lumn runat="server" HeaderText="Id Type Option" ">
<itemtemplate >
<asp:label runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"TypeOption ") %>' />
<asp:label runat="server" ID="LlbTypeOpti on" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>'/>
</itemtemplate>
<EditItemTempla te>
<asp:dropdownli st id="deViews" ?????="loaddd"
runat="server"> </asp:dropdownlis t>
<asp:label runat="server" Visible=False Text='<%#
DataBinder.Eval (Container.Data Item, "IdTypeOpti on") %>' />
</EditItemTemplat e>


Jul 21 '05 #10

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

Similar topics

0
1307
by: Stanley J Mroczek | last post by:
How do load a dropdownlist when edit is clicked in a datagrid and mark the selected item true? <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='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' />
3
828
by: Stanley J Mroczek | last post by:
I am trying to load a droplist in VB when the edit is clicked in a datagrid. I tried to use OnDataBinding and loading the droplist in subroutine "loaddd". I get this error Object reference not set to an instance of an object. Here is the code: <asp:TemplateColumn runat="server" HeaderText="Id Type Option" SortExpression="IdTypeOption"> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
1
1810
by: Stanley J Mroczek | last post by:
I am trying to load a dropdownlist in VB when the edit is clicked in a datagrid. How do i call Sub loaddd to load the dropdownlist <asp:TemplateColumn runat="server" HeaderText="Id Type Option" SortExpression="IdTypeOption"> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False Text='<%# DataBinder.Eval(Container.DataItem,...
1
2666
by: George Durzi | last post by:
When my datagrid is in edit mode, one of my columns is edited using a drop down list. I'm able to bind the DropDownList to a DataSource when in edit mode. HOWEVER, I can't preset the DropDownList's value when the Edit button is clicked for that row inside the datagrid. Here's a simplified version of my datagrid: <asp:datagrid id="dgHPU" runat="server"
12
658
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='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
0
9590
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6675
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.