Forms Data Controls :: Dynamic Gridview With Dropdownlist / Dynamically Set The List Values Based On Parameters (of The Row They Are On)

May 28, 2010

I'm new to web dev and c# so please bare with me. I am trying to create a dynamic gridview in a web form for users to to answer questions with (code below).

The dificulty im having is that i am nesting a dropdwonlist in the gridview and want to be able to dynamically set the list values based on parameters (of the row they are on). These values are in the main dataset for the gridview as each row represents a questionid and question text and then a ddl for the criteria...

I just don't know how to set the values for the dropdown all the code so far is below... just need to be able to populate the dropdowns with the relevant values.

I have created a stored proc to return the different criteria based on the questionid and questionGroupid which is the dataset that populates the other fields in the gridview: dbo.usp_QuestionCriteria @QuestionGroupId, @QuestionId

I have added this as a tableadapter called criteriaTableAdapter in a xsd file as well using the wizard... not sure if this is the right option though or just use the same method as i have for the other stored procedure as in the code below:

[CODE

]using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
con.Open();
SqlCommand com = new SqlCommand("usp_QuestionGroupDS", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(com);
DataSet ds = new DataSet();
ada.Fill(ds);
for (int i = 0; i < ds.Tables.Count; i++)
{
if (ds.Tables[i].Rows.Count > 0)
{
GridView gvDynamicQuestion = new GridView();
gvDynamicQuestion.Width = Unit.Pixel(700);
gvDynamicQuestion.BorderWidth = Unit.Pixel(0);
gvDynamicQuestion.Caption = "<div id="nifty" class="QuestionGroup"> <b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b>" + ds.Tables[i].Rows[0]["Category"].ToString() + " Questions<b class="rbottom"><b
class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b></div>";
gvDynamicQuestion.AutoGenerateColumns = false;
gvDynamicQuestion.ShowFooter = true;
TemplateField tf = null;
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.DataRow);
tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer, ds.Tables[i].Rows.Count);
gvDynamicQuestion.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.DataRow);
gvDynamicQuestion.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderText = "Criteria";
tf.HeaderTemplate = new DynamicGridViewTextTemplate("Criteria", DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewDDLTemplate();
gvDynamicQuestion.Columns.Add(tf);
////tf = new TemplateField();
////tf.HeaderText = "Criteria";
////tf.ItemTemplate = new DynamicGridViewDDLTemplate();
////gvDynamicQuestion.Columns.Add(tf);
gvDynamicQuestion.DataSource = ds.Tables[i];
gvDynamicQuestion.DataBind();
phDynamicGridHolder.Controls.Add(gvDynamicQuestion);
}
}
}
protected void DynamicGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
//
}
}
}
public class DynamicGridViewTextTemplate : ITemplate
{
string _ColName;
DataControlRowType _rowType;
int _Count;
public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
{
_ColName = ColName;
_rowType = RowType;
}
public DynamicGridViewTextTemplate(DataControlRowType RowType, int QuestionCount)
{
_rowType = RowType;
_Count = QuestionCount;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + _ColName + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
Label lbl = new Label();
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(lbl);
break;
case DataControlRowType.Footer:
Literal flc = new Literal();
flc.Text = "<b>Total No of Questions:" + _Count + "</b>";
container.Controls.Add(flc);
break;
default:
break;
}
}
private void lbl_DataBind(Object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString();
}
}
public class DynamicGridViewDDLTemplate : ITemplate
{
// Implementation of ITemplate
public void InstantiateIn(System.Web.UI.Control container)
{
// Create a DDL
DropDownList ddl = new DropDownList();
//Attach method to delegate
ddl.DataBinding += new System.EventHandler(this.ddl_DataBind);
container.Controls.Add(ddl);
}
//Method that responds to the DataBinding event
private void ddl_DataBind(object sender, System.EventArgs e)
{
//DropDownList ddl = (DropDownList)sender;
//DataGridItem container = (DataGridItem)ddl.NamingContainer;
//ddl.Data.Checked = [Data binding expression];
}
}

[/CODE]

View 7 Replies


Similar Messages:

Web Forms :: Dynamically Filter The Parameter's Values On A Popup Window Based On Other Parameters Selection

Apr 30, 2010

I have a pop up window which contains a listbox. The items on the listbox should be filtered when it is opened based on the other parameters on the parent page. I am using session to pass the values from a page to another.

View 4 Replies

Data Controls :: Sort GridView Using Dynamic Field Values From DropDownList

Mar 14, 2013

I have a gridview which is having coloumns Company name, Industry type, country, employees, revenue. i want to add dropdown at the top of each coloumn so that if user select industry as agriculture, all agriculture industry rows may be sorted, and same for other dropdown also.. i it may be possible,

View 1 Replies

Forms Data Controls :: Getting Values From A Dynamic Dropdownlist?

Jan 25, 2011

i have created a dynamic dropdownlist. i need to get the selected values from the list and store it in my sql database. i am new to asp.

View 5 Replies

Forms Data Controls :: Assigning Colour To List Items Based On Their Values?

Feb 9, 2010

Is it possible to assign a colour to a drop down list item based on it's value?For example: I have a drop downlist where I've got a bunch of items that have integer

values. I would like to highlight the items with values in 1000s in grey.

Here is what the list looks like:

list_item_name list_item_value
HEADING1 1000
HEADING2 1000
HEADING3 1000
item1 1
HEADING4 1000
item4 4
item5 5
HEADING5 1000
HEADING1 2000
HEADING2 2000
item2 2
item3 3
HEADING3 2000
HEADING4 2000
HEADING5 2000

I've boldified the items I'd like to be highlighted in grey ,For added complexity I need to highlight the items with values 1000 in a light shade of gray and items with values 2000 in a darker shade of grey. I'm using Web Developer, and C#

View 4 Replies

Web Forms :: Use Dynamically Created Controls Such As A Checkbox List And Fill The Values Dynamically?

Sep 28, 2010

I'm creating an quiz application which queries the database and extracts the questions and choices. I use dynamically created controls such as a checkbox list and fill the values dynamically. How do I do this? Right now I have these functions:

array_random_init(); this creates a 10 element integer array from 1-20 - extract_question(i): this extracts a question indexed at i in DB, I created the controls and set the appropriate text in this function. - validate_question(i); this is called by button_click and validates the question i according to DB.

I understand I have to recreate the controls in Page_init on postback, but what should it look like? Do I need to add "if IsPostBack" in page_init, or do I create the controls in page_init() and reset their properties in page_load when I use extract_question(i)? Also, I cannot seem to clear the selected boxes on postback, I added page directive "enableviewstate=false" but it doesn't work.

View 2 Replies

Data Controls :: Change Color Of DropDownList Based On Values

Apr 27, 2016

How can change the color of dropdownlist with based on their Text 

If  dropdownlist have  listitem Santosh  and Arati if santosh color is  red if Arati color is blue when page is loading

View 1 Replies

Forms Data Controls :: Gridview - Won't Return Anything At All Unless Provide Values For All 4 Parameters

Feb 25, 2011

i've got a gridview and sql datasource. Its connected to a stored proc that has four optional parameters. In VS when i set up the data source, if i use the test option, and dont supply any values for the sproc, it returns all the values in the table which is what it should do. Now when i try to fill the gridview, it wont return anything at all unless i provide values for all 4 parameters.

It's not even trying to search because i had sql profiler open and nothing shows up until i provide all 4 values. any idea why it's doing this? How do i get it to actually bind even if provide less then all 4 (or none) of the values?

View 1 Replies

How To Get The Individual Parameters From The List Of Dynamic Parameters In A Webmethod

May 12, 2010

I am using jquery ajax method on my aspx page,which will invoke the webmethod in the code behind.Currently the webmethod takes a couple of parameters like firstname,lastname,address etc which I am passing from jquery ajax method using

data:JSON.stringify({fname:firstname,lname:lastname,city:city})

now my requirement has been changed such that,the number and type of parameters that are going to be passed is not fixed for ex.parameter combination can be something like fname,city or fname,city or city,lname or fname,lname,city or something else.So the webmethod should be such that it should accept any number parameters.I thought of using arrays to do so, as described here.

But I do not understand how can I identify which and how many parameters have been passedto the webmethod to insert/update the data to the DB.

View 4 Replies

Forms Data Controls :: How To Get Column And Design In Gridview Dynamic Based On Value

Nov 18, 2010

I need to generate gridview based on table value... let us consider i have in my aspx page one dropdownlist in which i have binded somevalefor example: dropdownlist contain:- Item1 based on Item1 i have three table first table conists A,B,C,D value. Second Table consist 1,2,3,4,5 Third Table consist (0.5,0.6,0.7) for 1, (0.7,1.0) for 2, (0.6) for 3, (0.8) for 4, and another(0.8) for 5 based on this three table i need to show girdview like this which is below If its Item2 dropdownlist then row and column of girdview will be any number based on first,second,third table value for Item2like this if Item3 dropdownlist then row and column of girdview will be any number based on first,second,third table value for Item3.help me to achieve dynamic rows and column based on database value... in asp.net C#....

View 14 Replies

Web Forms :: Dynamic Controls Creation / Rendering Based On Postback Values

Nov 8, 2010

I had successfully created a functionality where I can dynamically add rows and columns to a asp table by creating/re-creating controls on every page load.

I have 2 buttons - Add rows and Add columns that, basically according to a asp.,net page life cycle, occur in the following order and work perfectly fine.

Page load is fired and controls along with their viewstate are recreated. In Add row button click event - I just create a new table row and add controls to each cell dynamically.

Same thing with Add column, Delete Row and Delete Column button click events.

However, now I added a Dropdown which when its index is changed needs to retrieve values from the database and create controls based on those values and then also bind the Db values.

But it doesn't work correctly since the asp.net page cycle cannot capture the dropdown's selected value in page_load as the page_load fired first and then the selectedindexchanged event is fired later, the controls are not created.

I don't know how to handle this scenario and incorporate it into my existing code.

View 3 Replies

Forms Data Controls :: Load A DropDownList Into A GridView With A Select Stored Procedure With Parameters

Sep 19, 2010

I want to have a dropdownlist in the footer row of a gridview. The dropdownlist must be populated with an stored procedure that receives a parameter and has a select. Is that posible?? I think yes but I having problems with the parameters.

[Code]....

The exception says incorrect sintax... but I've changed the procedure for something very simple to solve the problem like select 'hello' or select 1, 'hello' and I'm getting the same problem...

View 3 Replies

Forms Data Controls :: Filter Gridview Based On Value From Dropdownlist?

Aug 9, 2010

I have a gridview that I'd like to have changed based on a value selected in a dropdownlist. I'm able to get the aspx page to display the dropdown list (comming from SQL), and when I select a value, the page reloads, but no data show in the griview.

[Code]....

Here is the aspx page:

[Code]....

View 8 Replies

Forms Data Controls :: DropDownList With Dynamic Datasource In EditItemTemplate In Gridview?

Mar 2, 2011

I have two embedded dropdown lists in Gridview control. One gives you business channel options and the other represents products depending on the business channel selected.I am trying to use different data sources for product dropdown list depeding on what the user selected from the business channel drop down list. How could I do this. Here is my current code:

[Code]....

View 2 Replies

Forms Data Controls :: Display Gridview Based On Dropdown List Selection?

Feb 21, 2010

I'm having trouble figuring out how to display a gridview based on the selection of a drop down list. I manually entered the drop down list items, but in the configure/choose data source I don't understand how you make a relationship between each list item and the corresponding database table you want to show.

View 7 Replies

Data Controls :: Using DropDownList In Gridview And Bind Values In DropDownList

Oct 9, 2012

Using  dropdown in gridview and bind the values in dropdown then based on dropdown display the value in another column.

View 1 Replies

Forms Data Controls :: Display Some Xml Tags In Gridview Dynamically Based On User Selection?

Jul 17, 2010

i have displayed a xml file in the tree view form by binding them....now i want a sample code to access the nodes which are being selected and to display dynamically in a grid view ..in asp.net/c#

View 4 Replies

Forms Data Controls :: How To Display The Data Based On Dropdownlist Value In Gridview

Dec 30, 2010

I have a gridview.In Header of gridview i added dropdownlist.

[Code]....

Here the problem is, i am getting selected value in temp variable(if i change to 20,50,100) in dropdown selection event but it is not binding the pagesize for the gridview and also dropdownlist showing the value "10" only after the pageload.

View 6 Replies

Forms Data Controls :: Dynamically Alter The Page Size In A Paged Gridview Based On Data In A Particular Column?

Dec 14, 2010

I've already searched to no avail for an answer to this question:is it possible to dynamically alter the page size in a paged gridview based on data in a particular column?In other words,let's say I get data from a table that lists students enrolled in a particular course.If there are 10 students in Course "A," then I want page 1 in my gridview to show 10 records.

If there are 15 students in Course "B," then I want page 2 in my gridview to show those 15 records,and so on.I imagine that this could probably be done with a master-detail kind of set up,but let's say for the sake of argument that I don't want to go that route.

View 4 Replies

Forms Data Controls :: GridView Conditional Formatting Based On Duplicate Values?

Jun 2, 2010

I have a gridview containing serial-numbers.

I would like to format the background each data-row that contains a duplicate serial-number value.

I'm planning to do this using a DataBound event handler.

So, in English:

Set background colour of THIS_ROW to red where the Serial_Number of THIS_ROW is the same as the Serial_Number of ANY_OTHER_ROW.

All rows with duplicate serial-numbers would be red.

I'm not sure how to write the code for this event.

The gridview is bound to a SQL datasource. Getting the dupes in SQL is easy:

SELECT Serial
FROM Table
GROUP BY Serial
HAVING (COUNT(Serial) > 1)

View 8 Replies

Forms Data Controls :: Dynamically Assign Value To DropDownList In GridView

Sep 16, 2010

I got GridView with 2 columns both are Template field with drop down list. What I need to do is someting like when user select course 3/3A/2A/2B , the type column will auto select pratical option dynamically. When select RTT /FTT/BTT , the type column will select Theory option. Can someone tell me how to work on this thing.

<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField
HeaderText="Course">
<ItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem>2A</asp:ListItem>
<asp:ListItem>2B</asp:ListItem>
<asp:ListItem
Value="3">3</asp:ListItem>
<asp:ListItem>3A</asp:ListItem>
<asp:ListItem>BTT</asp:ListItem>
<asp:ListItem>RTT</asp:ListItem>
<asp:ListItem>FTT</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="Type">
<ItemTemplate>
<asp:DropDownList
ID="DropDownList2"
runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Practical</asp:ListItem>
<asp:ListItem>Theory</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

View 5 Replies

Forms Data Controls :: GridView Page Reset Based On DropDown List Selection?

Jan 3, 2011

i want to see the sales in the grid view based on the month selected from drop down list. Ex: when I select June 2010 from the ddl box, the sales for that month are correctly displayed in the grid view. I go to page 4 to look at some sales there and then switch the month to August 2010 to see sales for that month instead. Here is the problem. When I switch the month, it show August 2010 sales, but stays in page 4 instead of going back/resetting back to page 1 of August 2010 sales.

How do I reset the grid view page back to the first page of August 2010 sales?

I am fairly new to ASP. If I have missed an imp. detail in this post, let me know and I will get that info. out there.

View 4 Replies

Forms Data Controls :: Binding A Gridview Nested List Control Based On Field Value?

Dec 10, 2010

I need to bind a List control based on the Gridview Row's datakeyName value while the Gridview control is being loaded into the page. I tried the following but it does not work.The BulletedList control is nested inside a Gridview Control and they both bound to an ObjectDataSource control. In my code, I passed teh UserID into the UserID parameter and call the Select() method but still does not work. Basically, I want to list of a user's hobbies, therefore the hobbies control are driven by the userID. tell me how can I get it to work?

protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int UserID;

[code]...

View 1 Replies

Forms Data Controls :: Checkbox Selection Based On Group Column Values In Gridview?

Jul 28, 2010

I have to select checkboxes besed on group column values. If you see below gridview, column 3 (GroupN) has 1,1,1,1,2,2,2,2....etc (this column data is not static, will change based on page index. i.e PageIndex =2 may starts with 7,7,7,8,8,8,8,8,9,9,9 etc).

Now My question is.

1). If user selected '1', we have to store value and user must and should select another '1' (atleast two times).

2) If user selected '1', and user trying select '2'. Giving error says "Must have select one then one record in group to combine' (will not allow) and unselect '2'.

3) If user selected '1' atleast two times, and user trying select '2'. Will allow.

4) If user selected '1' atleast two times and selected '2' one time, trying to select '3'. will not allow user to select '3'.

View 3 Replies

Forms Data Controls :: Dynamically Populated Dropdownlist Inside Gridview

Mar 28, 2011

I have a gridview which contains a templatefield. Inside this is an ImageButton and a dropdownlist. This is all created in the aspx page, although the ddl list items are populated in the RowDataBound event in the vb page. When i change the selected item in the drop down list and hit the imagebutton, the selected item is not maintained after postback. Instead the first record is displayed in the dropdownlist. I have wrapped an If Not Page.IsPostback around the datasource and databind's but this doesn't help. I have tried enabling viewstate in all areas on the page too, but still nothing. The list items for the ddl must be created on RowDataBound also. As far as I am aware, my ddl is not really dynamic, its just the list items which I add in the code behind which are classed as dynamic.

View 11 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved