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)
{
CheckBox cbSelection = (CheckBox)sender;
GridViewRow row = (GridViewRow)cbSelection.NamingContainer;
TextBox CenterSequence = (TextBox)row.FindControl("CenterSequence");
if (cbSelection.Checked)
{
CenterSequence.Enabled = true;
var ValidSequences = grd_comm_grp.Rows.Cast<gridviewrow>().Where(a => ((TextBox)a.FindControl("CenterSequence")).Text != "").ToList();
if (ValidSequences.Count > 0)
CenterSequence.Text = (ValidSequences.Count + 1).ToString();
else
CenterSequence.Text = "1";
}
else
{
CenterSequence.Enabled = false;
CenterSequence.Text = "";
var ValidSequences = grd_comm_grp.Rows.Cast<gridviewrow>().Where(a => ((TextBox)a.FindControl("CenterSequence")).Text != "").OrderBy(a => ((TextBox)a.FindControl("CenterSequence")).Text).ToList();
if (ValidSequences.Count > 1)
for (int i = 0; i < ValidSequences.Count - 1; i++)
{
int InitialNumber = Convert.ToInt32(((TextBox)ValidSequences[i].FindControl("CenterSequence")).Text);
int NextNumber = Convert.ToInt32(((TextBox)ValidSequences[i + 1].FindControl("CenterSequence")).Text);
if (i == 0 && InitialNumber > 1)
{
((TextBox)ValidSequences[i].FindControl("CenterSequence")).Text = "1";
InitialNumber = 1;
}
if (InitialNumber + 1 != NextNumber)
((TextBox)ValidSequences[i + 1].FindControl("CenterSequence")).Text =(InitialNumber + 1).ToString();
}
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