Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

binding dropdownlist to datagrid

  Asked By: Asksuresh    Date: Nov 27    Category: Asp.net    Views: 2920
  

I'm trying to add a dropdownlist to a datagrid. But i'm using code-behind and i may not use code in my html form.
All examples i found online are using a function that is standing into the html form

So i'm trying to do it on another way and getting all the times fault on the line "myDataTable = myDataSet.Tables("Tabel")"
(Object reference not set to an instance of an object)
while i'm doing exactly the same as binding the dataTable on a normal datagrid with a click on a button...
I don't guess this is the reason, but causod bij those ItemDataBound stuff...

Could some one please help me out with this?
And if succedeed, how can i select the data selected in the rule of the datagrid, containing the value of the dropdownlist?

CODE SNIPPET:


Public
Sub eventItemDataBound1(ByVal S As Object, ByVal e As DataGridItemEventArgs)

Dim drop1 As DropDownList = e.Item.FindControl("myDDL")
Dim myDataSet As DataSet
Dim myDataTable As DataTable
myDataSet =
CType(Session("data"), DataSet)
myDataTable = myDataSet.Tables("Tabel") '=> HERE I'VE GOT PROBLEMS
Dim myDataView As DataView
myDataView =
New DataView(myDataTable)

drop1.DataSource = myDataSet
drop1.DataMember = "Tabel"
drop1.DataTextField = "joke"
drop1.DataValueField = "aantal"
drop1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myDataSet As DataSet
Dim myDataTable As DataTable
myDataSet = CType(Session("data"), DataSet)
myDataTable = myDataSet.Tables("Tabel") '=> HERE NO PROBS...
Dim myDataView As DataView
myDataView = New DataView(myDataTable)
DataGrid2.DataSource = myDataView
DataGrid2.DataMember = "Tabel"
DataGrid2.DataBind()

End Sub

<form id="Form1" method="post" runat="server">
<asp:DataGrid OnItemDataBound="eventItemDataBound1" id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="joke" HeaderText="nummer" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList Runat="server" ID="myDDL" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
</form>

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Boyce Fischer     Answered On: Nov 27

Could you include the code  complete code where the dataset  is stored to
session.

I suspect from the error you are getting, is that when the
eventItembound is run, the session variable is empty

And when the Button_click1 event, which would happen on a post back, the
session variable has been set.

 
Answer #2    Answered By: Stacy Cunningham     Answered On: Nov 27

this is my entire testcode... What you're saying could be true... but what if i want to do this?
Or is there a manner to put to dataSet global... I've been looking a lot after ways to set  the dataSet global but i didn't succeed,
The problem is that i normally may not use Sessions....

 
Answer #3    Answered By: Jimmie Ramirez     Answered On: Nov 27

In the page on load event, you need to save the dataset  to session
before calling the databind method.

 
Answer #4    Answered By: Gilberto Thompson     Answered On: Nov 27

could you please give me a help, cos i'm really don't see how i have to do that...
I've tried to place de Session("data")= myDataSet before Databinding on the datagrid, but this make no sence...

which specific things i've to change / add?

 
Answer #5    Answered By: Abbas Hashmi     Answered On: Nov 27

Where you have this code  in the page load event


DataGrid1.DataSource = myDataSet
DataGrid1.DataMember = "Tabel"
DataGrid1.DataBind()
Session("data") = myDataSet


You are setting the datasource  and then calling the databind() method

When you call the dataBind() immediately, the onItemDataBound event is
called for each row of data.



In the itemDataBoundEvent, you are attempting to retrieve the dataset
from the session variable but at this point, when the page is first
loaded, you have not put anything in the session variable, when you
attempt to get the table from the dataset  hence the error.

If I change the code in the page load event to look like this instead,

Session("data") = myDataSet
DataGrid1.DataSource = myDataSet
DataGrid1.DataMember = "Tabel"
DataGrid1.DataBind()

Now when the item data  bound event is called, I am sure to have the data
in the session variable.

 
Answer #6    Answered By: Jana Franklin     Answered On: Nov 27

i've done that but i'm now getting errors (obj refer is not set) from the drop1 in the Sub eventItemDataBound...
Is my ItemTemplate OK or not?

 
Answer #7    Answered By: Clay Cook     Answered On: Nov 27

When ever you are working with grids, you need to make sure when you do
the findcontrol method that the control actually exists on that row of
the grid.

What trips up a lot of people when working with the OnItemEvents is the
header row and the footer row are run through here as well and as such
do not contain the controls you are looking for.


Dim drop1 As DropDownList = e.Item.FindControl("myDDL")
If not isNothing(drop1) then

Dim myDataSet As DataSet

Dim myDataTable As DataTable

myDataSet = CType(Session("data"), DataSet)

myDataTable = myDataSet.Tables("Tabel")

Dim myDataView As DataView

myDataView = New DataView(myDataTable)

drop1.DataSource = myDataView

drop1.DataMember = "Tabel"

drop1.DataTextField = "joke"

drop1.DataValueField = "aantal"

drop1.DataBind()

End if

 
Didn't find what you were looking for? Find more on binding dropdownlist to datagrid Or get search suggestion and latest updates.




Tagged: