Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning the Correct Value to a Gridview's Dropdown Template 1

Status
Not open for further replies.

majors479

Technical User
Jul 5, 2008
16
US
Hi,

I am attempting to add a dropdown to a an edit template field in a gridview whose data source is table 1. The field is an integar field that is a foreign key to table 2's primary key. I'm binding the primary key and description field from table 2 as the data value and shown value in the dropdown field (as I would in Access). When I do this, I receive the value of table 2's primary key field and not the value that is in the gridview's data source's field (table 1's foreign key to table 2)that I have turned into a dropdown field.

I would like the user to see the description field in the dropdown from table 2, but keep it binded to table 1's original field's value.

Any suggestions on how to accomplish this will be greatly appreciated.

Sydney
 
you must provide code. that said your problem is your setting the selected index before the table is bound to the drop down list.
something like this should work
Code:
protected void SetDropDownValues(object sender, GridViewRowDataBoundEventArgs e)
{
   if(e.Row.RowType !=  DataRowType.DataRow) return;
   if(e.Row.RowState !=  RowStateType.Edit) return;

   DataRowView row = e.Row.DataItem as DataRowView;
   string id = row["name of column"].ToString();
   DropDownList list = e.Row.FindControl("name of control") as DropDownList;
   if(list == null) return;
    
   list.DataSource = GetData();
   list.DataBind();
   list.Items.FindByValue(id).Selected = true;
}
Code:
<asp:GridView OnRowDataBound="SetDropDownValues">
   <Columns>
      <asp:TemplateColumn>
          <EditTemplate>
               <asp:DropDownList DataValueField="name of value column" DataTextField="name of value column" />
          </EditTemplate>
      </asp:TemplateColumn>
   <Columns>
</asp:GridView>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason,

Thanks so much for your help. I've attempted to change your Behind code from C# to VB, but am getting an error saying that "Name 'DataRowType' is not declared." So far I have the following

Code:
Protected   Sub SetDropDownValues(ByVal sender As object, ByVal e As System.Web.UI.WebControls.GridViewrowEventArgs)
   if(e.Row.RowType = DataRowType.DataRow) then
   if(e.Row.RowState = RowStateType.Edit) then

   DataRowView row = e.Row.DataItem as DataRowView
   string id = row["Companycode"].ToString()
   DropDownList list = e.Row.FindControl("dropdownlist2") as DropDownList
   if(list =null) then
    
   list.DataSource = GetData()
   list.DataBind()
   list.Items.FindByValue(id).Selected = true
        end if
    end if 
end if
    end sub
Not sure if the end ifs are in the right place.

Again, Thanks so much for your assistance.

Sydney
 
DataRowType is named something else. i can't remember off the top of my head. it's a enum. maybe just RowType or something.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I got through the datarowtype issue, but am now getting an error on the
Code:
  dataRowView row = e.Row.DataItem as datarowview
line. It states that name datarowview is not declared. I can't find any associated name in VB. I currently have
Code:
Protected Sub SetDropDownValues(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.RowState = DataControlRowState.Edit) Then
                dataRowView row = e.Row.DataItem as datarowview  _
                string id = row["Companycode"].ToString() _
                DropDownList list = e.Row.FindControl("dropdownlist2") as DropDownList _
                If (list = null) Then _
                list.DataSource = GetData() _
                list.DataBind() _
                list.Items.FindByValue(id).Selected = True
            End If
        End If

    End Sub

Also, still not sure if the end ifs are in the correct place.

Thanks for all of your help. I really aprreciate it.

Sydney
 
I used this online source to convert the code:

Code:
Protected Sub SetDropDownValues(ByVal sender As Object, ByVal e As GridViewRowDataBoundEventArgs) 
    If e.Row.RowType <> DataRowType.DataRow Then 
        Return 
    End If 
    If e.Row.RowState <> RowStateType.Edit Then 
        Return 
    End If 
    
    Dim row As DataRowView = TryCast(e.Row.DataItem, DataRowView) 
    Dim id As String = row("name of column").ToString() 
    Dim list As DropDownList = TryCast(e.Row.FindControl("name of control"), DropDownList) 
    If list Is Nothing Then 
        Return 
    End If 
    
    list.DataSource = GetData() 
    list.DataBind() 
    list.Items.FindByValue(id).Selected = True 
End Sub
 
Thanks for the conversion. That's a cool tool. I made some changes to the rowstate and datarowtype to better fit VB, but still can't find anything in VB that is associated with DataRowview. Thanks to your conversion I currently have the following, which is throwing an error on DataRowview.

Code:
    Protected Sub SetDropDownValues(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType <> DataControlRowType.DataRow Then
            Return
        End If
        If e.Row.RowState <> DataControlRowState.Edit Then
            Return
        End If

        Dim row As DataRowView = TryCast(e.Row.DataItem, DataRowView)
        Dim id As String = row("Companycode").ToString()
        Dim list As DropDownList = TryCast(e.Row.FindControl("dropdownlist2"), DropDownList)
        If list Is Nothing Then
            Return
        End If

        list.DataSource = LinqDataSource1()
        list.DataBind()
        list.Items.FindByValue(id).Selected = True
    End Sub

Thanks again for your help.

Sydney
 
Sorry, I should have been more clear. The error is on the below line and states that "DataRowView is not a declared name". I had the same issue with rowtype and I changed it to DataControlRowType, but I can't find a corresponding one for DataRowView
Code:
Dim row As DataRowView = TryCast(e.Row.DataItem, DataRowView)

Thanks,

Sydney
 
There are a couple of ways to fix this, and VS intellesense can help you. I just added:
Code:
Imports System.Data
 
That solved the DataRowView issue, but now I'm getting an error on the next line
Code:
    Dim id As String = row("Companycode").ToString()

That states "System.NullReferenceException: Object reference not set to an instance of an object."
 
Thanks for your help and patience. Sorry, but I'm missing something.

I'm assuming this line of code is reading the field's data company ID from contact table) and not the data that belongs to the associated datasource that the dropdown control is using (which is the company ID and company name from the company table). If so, then the field isn't null.

Thanks again

Sydney
 
It is reading it from the current row in the datasource
Code:
Dim row As DataRowView = TryCast(e.Row.DataItem, DataRowView)
Once the page retrieves the data, it populates some sort of datasource, in this case, a dataview. Your object(dropdown) is not directly conneted to the db.

The error is telling you that field, in that row is NULL. You will have to check for NULLs to avoid this error. If you are saying this column should never be NULL, then you need to check the data that you are getting from this sub/function: LinqDataSource1()
.

Looks like you will need to do some debugging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top