GridView with a dropdownList in atemplate column

Discussion in 'Software' started by aidoCo, Oct 13, 2006.

  1. aidoCo


    Oct 13, 2006

    really really hoping someone can help with this , by modifying the my code to return values to my GridView's templated column containg a dropdownlist.

    I am trying to present a list of questions each of which can have different Answer types.

    For example some questions can be answered with Yes and No and some can answered with be Yes, No, Not applicable, Exceeds.
    To handle this I have 3 tables..

    tblQuestion (QuestionID, QuestionText)
    tblAnswer (AnswerID,Answertext)
    tblAllowedAnswer (QuestionID,AnswerID)

    I have a grid view that returns the values from tblQuestion(select * from tblquestion)

    I added a templated field to the Gridview which contaisn a dropdown list , I now want to bind this dropdownlist to the allowable answers for each question ID (the QuestionID from that row of the GridView)

    i think i need an event handler for RowDataBound as suggested by Azamsharp in his articles on but I don't know how to write the event handler

    Any idea of the code I would need to add to GridView1_RowDataBound to return the relevant values from tblAllowedAnswer to the templated fields dropdownlist

    Attached is the aspx page source, Any ideas are very much appreciated.

    aspx page....

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <html xmlns="" >
    <head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    &nbsp;<asp:Label id="Label1" runat="server" Text="Name" Font-Bold="True" Width="107px"></asp:Label>
    <asp:TextBox id="TextBox1" runat="server" Width="77px" Height="12px"></asp:TextBox>
    <br />
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" Width="80%">
    <asp:BoundField DataField="QID" HeaderText="QID" InsertVisible="False" ReadOnly="True"
    SortExpression="QID" />
    <asp:BoundField DataField="Qtext" HeaderText="Qtext" SortExpression="Qtext" />
    <asp:TemplateField ShowHeader="False">
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2">
    <SelectedRowStyle BackColor="Yellow" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CPL_CallCentreConnectionString %>"
    SelectCommand="SELECT * FROM [tbl1Q]"></asp:SqlDataSource>
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CPL_CallCentreConnectionString %>"
    SelectCommand="SELECT * FROM [tbl1Q]"></asp:SqlDataSource>
    <br />
    <asp:Button ID="Button4" runat="server" Text="submit" OnClick="Button4_Click" /><br />
    Last edited: Oct 13, 2006
    aidoCo, Oct 13, 2006
    1. Advertisements

  2. aidoCo

    John Rieder

    Sep 18, 2007
    rowdatabound caching first row ??

    I have a related problem. I have two drop down lists that use data in their row to populate them. Have done due diligence to make this work...okay. They populate as expected with one exception The dropdown lists in the first row are empty, and the expected 'list' for that row shows up in the drop down box on the second row...and so on and so on...seems all the ddl's content is off by one row. Stepping through code I also notice that I cannot set the Selected value for the first rowdatabound row ddl's it is in the cache but the ddl does not populate on the first item...maybe a postback then occurs and all my ddls from 2nd row down are populated and selected?

    Just what is occurring here? I will gladly post code from my rowdatabound below...Thanks for any assistance.

    if (e.Row.RowType == DataControlRowType.DataRow)

    // get project code for work code ddl
    string ProjectCode = DataBinder.Eval(e.Row.DataItem, "ProjectCode").ToString();
    // populate work code ddl

    workCodeId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "WorkCodesId"));
    DropDownList ddl = (DropDownList)e.Row.FindControl("WorkCodeDropDown");

    ddl.SelectedValue = workCodeId.ToString();

    // get rate from column
    decimal rate = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "BillRate"));

    // get project Id from column
    ProjectId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ProjectId"));
    // get employee id from column
    EmployeeId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "EmployeeId"));
    //populate rate sheet ddl
    _presenter.SetViewRateSheetGridDropDown(ProjectId, EmployeeId);
    DropDownList ddl2 = (DropDownList)e.Row.FindControl("RateSheetDropDown");
    ddl2.SelectedValue = rate.ToString();

    ApprovalId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ApprovalCodeId"));
    John Rieder, Sep 18, 2007
    1. Advertisements

  3. aidoCo


    Sep 20, 2007
    Gridview index is not updating after using filterexpression

    Hi, Hope someone can help with this:
    I have a default filter to generate gridview databinding. After the resultset 1 is displayed, I allow user to further filter out with two additional filters (combo box selection), the two filters are added by using filterexpression. Then, the resultset 2 is displayed without problem. But if Click on Edit button on a row, I found the edit row opened is not the one I clicked but the one in the same order from the previous result set. See I clicked resultset 2 on the 3rd row to edit, the row showing edit mode is the 3rd row on the resultset 1.
    How is this to be fixed?
    Lino, Sep 20, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. visj4u

    DropdownList in gridview

    visj4u, Apr 27, 2007, in forum: Software
    Apr 27, 2007
  2. VisualStudio2005VBASPX

    Help needed with getting selected-column-index of GridView

    VisualStudio2005VBASPX, Sep 26, 2007, in forum: Software
    Sep 26, 2007
  3. WillC999

    ASP GridView hyperlinks driving me insane!

    WillC999, Oct 28, 2008, in forum: General Computer Support
    Oct 28, 2008
  4. london1919
    Lawrence Garvin \(MVP\)
    Dec 8, 2008
  5. Chandu

    .Net, binding dropdownlist

    Chandu, Jun 2, 2011, in forum: Software
    Jun 2, 2011