My client requirement is - In a gridview there is a dropdown called "Rule".Now there is a Add button outside grid.On click of add button gridview generates one more row.I need to calculate total row number and take module(%2) with precision value(if row
no is 5 the 5%2=2) to get the Rule value and need to bind it with dropdown.
that means: initially grid has 1 row and Rule ddlist has value 1. Now on adding 1 more row grid row count becomes 2
now grid has 2 row and Rule ddlist will have value 1
like wise when grid will have 4 rows then Rule ddlist should have 1,2 (because 4%2=2) for 6 rows value should have 1,2,3.
Hmmmmm, you seem to need the result of integer division, not the remainder of integer division. (Modulo is the remainder of integer division. 5 % 2 = 1. 4 % 2 = 0.)
Have you succeeded in adding a Row? If so, how did you do it?
Typically, it is done using a DataBind each time a new Row is done. Since you need the total number of Rows, I think you had best use the DataBound event. In the handler, loop over the Rows collection. Find the DropDownList in each Row, and DataBind it.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
kook23
Member
26 Points
39 Posts
Gridview dropdown binding based on rule
Aug 03, 2012 07:52 PM|LINK
My client requirement is - In a gridview there is a dropdown called "Rule".Now there is a Add button outside grid.On click of add button gridview generates one more row.I need to calculate total row number and take module(%2) with precision value(if row no is 5 the 5%2=2) to get the Rule value and need to bind it with dropdown.
that means: initially grid has 1 row and Rule ddlist has value 1. Now on adding 1 more row grid row count becomes 2
now grid has 2 row and Rule ddlist will have value 1
like wise when grid will have 4 rows then Rule ddlist should have 1,2 (because 4%2=2) for 6 rows value should have 1,2,3.
I need help or some code snippet.
ganesh.rane
Participant
1854 Points
318 Posts
Re: Gridview dropdown binding based on rule
Aug 04, 2012 05:15 AM|LINK
Please try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
int count = index / 2;
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
for (int i = 0; i < count; ++i)
{
ddl.Items.Add(i.ToString());
}
}
}
Please mark this post as Answer if this helped you.
Ganesh Rane
Don't forget to mark useful responses as Answer if they helped you towards a solution.
superguppie
All-Star
48225 Points
8679 Posts
Re: Gridview dropdown binding based on rule
Aug 06, 2012 09:11 AM|LINK
Hmmmmm, you seem to need the result of integer division, not the remainder of integer division. (Modulo is the remainder of integer division. 5 % 2 = 1. 4 % 2 = 0.)
Have you succeeded in adding a Row? If so, how did you do it?
Typically, it is done using a DataBind each time a new Row is done. Since you need the total number of Rows, I think you had best use the DataBound event. In the handler, loop over the Rows collection. Find the DropDownList in each Row, and DataBind it.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.