从c#中的最后一行循环到gridview

时间:2014-10-29 06:18:57

标签: c# asp.net gridview itemtemplate

我正在创建一组学生。我在c#asp.net中有一个Gridview。它包含一个项目模板字段标签。我有一个下拉列表,我从中选择否。我希望在网格视图中应用循环,以便当我从下拉列表中选择任何整数值时,项目模板字段标签获取值为....示例 如果我从下拉列表中选择3然后循环工作,因为它首先为前3名学生分配值,然后移动到gridview的最后并从下到上分配值3学生并再次从顶部移动等等......

name       groupno.

A             1   
B             2
C             3
D             1
E             2
F             3 
G             1
H             3
I             2
J             1
K             3
L             2
M             1

请帮帮我。

1 个答案:

答案 0 :(得分:0)

protected void Button1_Click(object sender, EventArgs e)
{
        DataTable dt1;
        DataRow row; 
        dt1 = new DataTable();
        dt1.Columns.Add("GroupNo");
        dt1.Columns.Add("Registration");
        dt1.Columns.Add("Name");
        dt1.Columns.Add("Marks");
        dt1.Columns.Add("Technology");
        dt1.AcceptChanges();
        return dt1;

        int starting = 0, Ending = GridView1.Rows.Count-1, flag = 0, groupsize =5 , Count_Stu = 1;

        for (int i = 0; i <= countvalue; i++)
        {
            if (flag == 0)  // Top To Down
            {
                row = dt1.NewRow();
                row["GroupNo"] = Count_Stu.ToString();
                row["Registration"] = GridView1.Rows[starting].Cells[0].Text;
                row["Name"] = GridView1.Rows[starting].Cells[1].Text;
                row["Marks"] = GridView1.Rows[starting].Cells[2].Text;
                row["Technology"] = GridView1.Rows[starting].Cells[3].Text;
                dt1.Rows.Add(row);
                Count_Stu++;
                starting++;
            }
            else if (flag == 1) // Down To Up
            {
                row = dt1.NewRow();
                row["GroupNo"] = Count_Stu.ToString();
                row["Registration"] = GridView1.Rows[Ending].Cells[0].Text;
                row["Name"] = GridView1.Rows[Ending].Cells[1].Text;
                row["Marks"] = GridView1.Rows[Ending].Cells[2].Text;
                row["Technology"] = GridView1.Rows[Ending].Cells[3].Text;
                dt1.Rows.Add(row);
                Count_Stu++;
                Ending--;
            }

            if (Count_Stu == groupsize+1)   //Reset 
            {
                if (flag == 0)
                    flag = 1;
                else
                    flag = 0;
                Count_Stu = 1;
            }
        }
        GridView2.DataSource = dt1;
        GridView2.DataBind();

    }