Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > Software > ASP.net GridView with a dropdownList in atemplate column

Reply
Thread Tools

ASP.net GridView with a dropdownList in atemplate column

 
 
aidoCo aidoCo is offline
Junior Member
Join Date: Oct 2006
Posts: 1
 
      10-13-2006
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>
<aspropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2">
</aspropDownList>
</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 by aidoCo; 10-13-2006 at 01:26 PM..
Reply With Quote
 
 
 
 
John Rieder John Rieder is offline
Junior Member
Join Date: Sep 2007
Posts: 1
 
      09-18-2007
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"));
 
Reply With Quote
 
 
 
 
Lino Lino is offline
Junior Member
Join Date: Sep 2007
Posts: 1
 
      09-20-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why A Big Difference between C++98 and C++0x on a local type as atemplate-argument? xmllmx C++ 6 02-07-2010 05:03 PM
gcc 4 can not resolve member var derived from base class in atemplate class steve yee C++ 5 07-01-2008 09:31 PM
gridview column values to another page via gridview hyperlink column Keith G Hicks ASP .Net 3 02-18-2008 06:17 AM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
how to Add different controls(textBox,DropDownList or some ) in the same column,based upon the value in the previous column (Say second Colum which contain dropdown with some values) ? Balaji ASP .Net Datagrid Control 0 07-07-2003 08:32 AM



Advertisments