Help with Drop Down in Datagrid

LaTTicE

Member
Joined
Jun 14, 2004
Messages
11
Programming Experience
1-3
I have a drop down list in my datagrid that gets loaded when the edit button in the editcommandcolumn is clicked. The datasource property of the drop down is set to call a function to load this drop down. However, I also have the selectedindex attribute calling a different function that gets the correct index of the drop down list for whatever value is in that column of the datagrid. The problem is, this only works if there is a value in that column of the datagrid. If there isn't, I get a DBNull error message. I need to be able to click on a row that doesn't have a value so that I can pick one to update it with. Thanks for any help in advance.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
You can usually overcome that problem by checking to see if the value is DBNull before performing calculations on the value. For example:

VB.NET:
If Value <> System.DBNull.Value then ...
 

LaTTicE

Member
Joined
Jun 14, 2004
Messages
11
Programming Experience
1-3
Problem

I wish it were as simple as that... I have already tried doing that in every part of the program I can think of that affects this, but to no avail. Anyway, I thought I might include a code snippet this time to see if that could help any. Thanks once again for any help... Here is the code along with the drop down list code that I call these two functions from in my datagrid. The only thing I've noticed is that if the value is null that I need to get, which means not to get it and just load the drop down, it throws the error right when it gets to the selectedindex part. I just need a way to see if it's null so it knows whether or not to call that function for the selected index.

VB.NET:
[size=2][color=#008000]'Function used to load drop down in datagrid

[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] GetEquipName() [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] connection [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strSQL [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String

[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strConn [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String

[/color][/size][size=2]strConn = "bla"

connection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection(strConn)

strSQL = "SELECT EqName,EqTypeID FROM tlkpEquipment"

connection.Open()

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] SQLDSET [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DataAdapter [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataAdapter = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlDataAdapter(strSQL, connection)

[/size][size=2]DataAdapter.Fill(SQLDSET, "tlkpEquipment")

connection.Close()



[/size][size=2][color=#0000ff]Return[/color][/size][size=2] SQLDSET

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function[/color][/size]
[size=2][/size][size=2][color=#008000]'Function used to get correct index for drop down

[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] GetEqTypeIndex([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] EqTID [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer

[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] iLoop [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer

[/color][/size][size=2][color=#008000]'Loop through each row in the DataSet

[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] dt [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataTable = GetEquipName.Tables("tlkpEquipment")

[/size][size=2][color=#0000ff]For[/color][/size][size=2] iLoop = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] dt.Rows.Count - 1

[/size][size=2][color=#0000ff]If[/color][/size][size=2] Int32.Parse(EqTID) = Int32.Parse(dt.Rows(iLoop)("EqTypeID")) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] iLoop

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If

[/color][/size][size=2][color=#0000ff]Next[/color][/size][size=2] iLoop

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function

[/color][/size]

HTML:
<asp:templatecolumn HeaderText="Equipment Name">
 <ItemTemplate>
  <%# DataBinder.Eval(Container.DataItem(), "EqName") %>
 </ItemTemplate>
 <edititemtemplate>
  <asp:DropDownList id="ddldg" runat="server" DataSource='<%# GetEquipName() %>' DataTextField="EqName" DataValueField="EqTypeID"  SelectedIndex='<%# GetEqTypeIndex(Container.DataItem("EqTypeID"))%>'/>
 </edititemtemplate>
</asp:templatecolumn>
 
Top Bottom