ASP.net GridView with a dropdownList in atemplate column

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

  1. aidoCo

    aidoCo

    Joined:
    Oct 13, 2006
    Messages:
    1
    Likes Received:
    0
    Hi,

    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 GridViewGuy.com 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    &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 />
    </div>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" Width="80%">
    <Columns>
    <asp:BoundField DataField="QID" HeaderText="QID" InsertVisible="False" ReadOnly="True"
    SortExpression="QID" />
    <asp:BoundField DataField="Qtext" HeaderText="Qtext" SortExpression="Qtext" />
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2">
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <SelectedRowStyle BackColor="Yellow" />
    </asp:GridView>
    <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 />
    &nbsp;
    </form>
    </body>
    </html>
     
    Last edited: Oct 13, 2006
    aidoCo, Oct 13, 2006
    #1
    1. Advertisements

  2. aidoCo

    John Rieder

    Joined:
    Sep 18, 2007
    Messages:
    1
    Likes Received:
    0
    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
    _presenter.SetViewWorkCodeGridDropDownProjectCode(ProjectCode);

    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
    #2
    1. Advertisements

  3. aidoCo

    Lino

    Joined:
    Sep 20, 2007
    Messages:
    1
    Likes Received:
    0
    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?
    Thanks.
    Lino
     
    Lino, Sep 20, 2007
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.