Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
a grid view in that a column for checkbox and second column for textboxes .
when i check the checkbox then textbox shows sequence of the checkbox.when i first time check on the checkbox then textbox shows the 1, and next time check the checkbox then textbox shows the 2.and so on.......
when i unselect the checkbox then sequence are rearranged.for example when i unselect the checkbox sequence is 3 then rearranged the sequence 1,2,4 becomes 3,5 becomes 4 and so on.
protected void grd_comm_grp_SelectedIndexChanged(object sender, EventArgs e)
    {      
        //Find the CheckBox and get the row 
        CheckBox cbSelection = (CheckBox)sender;
        GridViewRow row = (GridViewRow)cbSelection.NamingContainer;
        TextBox CenterSequence = (TextBox)row.FindControl("CenterSequence");
        // If Checked 
        if (cbSelection.Checked)
        {
            // Enable the TextBox 
            CenterSequence.Enabled = true;
            // Find Rows with sequence value 
            var ValidSequences = grd_comm_grp.Rows.Cast<gridviewrow>().Where(a => ((TextBox)a.FindControl("CenterSequence")).Text != "").ToList();
            // If there are any sequences, increment by 1 
            if (ValidSequences.Count > 0)
                CenterSequence.Text = (ValidSequences.Count + 1).ToString();
            // If new, put 1 
            else
                CenterSequence.Text = "1";
        }
        // If unchecked, clear the text, disable it and reset the sequence 
        else
        {
            CenterSequence.Enabled = false;
            CenterSequence.Text = "";
            // Find rows with sequence values 
            var ValidSequences = grd_comm_grp.Rows.Cast<gridviewrow>().Where(a => ((TextBox)a.FindControl("CenterSequence")).Text != "").OrderBy(a => ((TextBox)a.FindControl("CenterSequence")).Text).ToList();
           // If more than one row, reset the sequence 
            if (ValidSequences.Count > 1)
                // loop until last but one 
                for (int i = 0; i < ValidSequences.Count - 1; i++)
                {
                    // check if the difference between the current number and next number is one. 
                    int InitialNumber = Convert.ToInt32(((TextBox)ValidSequences[i].FindControl("CenterSequence")).Text);
                    int NextNumber = Convert.ToInt32(((TextBox)ValidSequences[i + 1].FindControl("CenterSequence")).Text);
                    
                   // First one in the sequence should be 1 
                   // If sequence does not start from 1, make it 1 
                    if (i == 0 && InitialNumber > 1)
                    {
                        ((TextBox)ValidSequences[i].FindControl("CenterSequence")).Text = "1";
                         InitialNumber = 1;
                    }
                    // If the difference is not 1, make the difference 1 
                    // Ex: 1 and 3 make 3 as 2 
                    if (InitialNumber + 1 != NextNumber)
                        ((TextBox)ValidSequences[i + 1].FindControl("CenterSequence")).Text =(InitialNumber + 1).ToString();
                }
            // If only one element, make the sequence 1 
            else if (ValidSequences.Count == 1)
                ((TextBox)ValidSequences.First().FindControl("CenterSequence")).Text = "1";
        }       
    }

problem is that when sequence is above from 9 then NextNumber contain the second element is 10 instead of 2 . The problem in this line
int NextNumber = Convert.ToInt32(((TextBox)ValidSequences[i + 1].FindControl("CenterSequence")).Text);

i think problem in line
var ValidSequences = grd_comm_grp.Rows.Cast<gridviewrow>().Where(a =>(((TextBox)a.FindControl("CenterSequence")).Text) != "").OrderBy(a => ((TextBox)a.FindControl("CenterSequence")).Text).ToList();

because var ValidSequences contain the index of the textbox list .it gives the validSequence[0]=1 ,validSequences[1]=10 instead of ValidSequence[1]=2
Posted
Updated 2-Aug-11 19:49pm
v4
Comments
Herman<T>.Instance 2-Aug-11 4:09am    
if you debug what is the value of
<pre>ValidSequences[i + 1].FindControl("CenterSequence")</pre>
amit646 2-Aug-11 4:34am    
it gives the error
"Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900