一页中有多个Gridview

时间:2014-09-26 08:30:32

标签: c# asp.net .net gridview grid

我想在一页中使用2个Gridview
第一个gridview应显示在页面加载上,第二个应显示在每行第一个gridview内点击链接按钮

设计中的第一个gridview

     <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" onrowcancelingedit="gvDetails_RowCancelingEdit" DataKeyNames="Class_ID,Class_ShortName" 
        onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing" AllowPaging="true" PageSize="5" OnPageIndexChanging="gvDetails_PageIndexChanging"
        onrowupdating="gvDetails_RowUpdating" CssClass="grid">
            <Columns>

            <asp:TemplateField HeaderText="Class ID" Visible="false">
         <ItemTemplate>
         <asp:Label ID="a1" runat="server" Text='<%#Eval("Class_id") %>'/>
          </ItemTemplate>
         </asp:TemplateField>
            <asp:TemplateField HeaderText="Select" ControlStyle-Width="80px">
         <ItemTemplate>

             <asp:LinkButton ID="LinkButton1" runat="server"  OnClick="selectrow"  CommandArgument = "LinkButton1" Text='<%#Eval("Class_id") %>'> </asp:LinkButton> 
          </ItemTemplate>
          </asp:TemplateField>




         <asp:TemplateField HeaderText="Enrolment Fee" ControlStyle-Width="100px" >
         <EditItemTemplate>
        <asp:TextBox ID="a2" runat="server" Text='<%#Eval("Enrolment_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a21" runat="server" Text='<%#Eval("Enrolment_fee") %>'/>
          </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="Costume Fee"  >
         <EditItemTemplate>
        <asp:TextBox ID="a3" runat="server" Text='<%#Eval("Costume_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a31" runat="server" Text='<%#Eval("Costume_fee") %>'/>
          </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="DSA Academy Fee" ControlStyle-Width="80px">
         <EditItemTemplate>
        <asp:TextBox ID="a4" runat="server" Text='<%#Eval("DSA_Academy_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a41" runat="server" Text='<%#Eval("DSA_Academy_fee") %>'/>
          </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Ensemble1 Fee" ControlStyle-Width="100px">
         <EditItemTemplate>
        <asp:TextBox ID="a5" runat="server" Text='<%#Eval("Ensemble1_fee") %>' />
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a51" runat="server" Text='<%#Eval("Ensemble1_fee") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Ensemble2 Fee"  ControlStyle-Width="100px">
         <EditItemTemplate>
        <asp:TextBox ID="a6" runat="server" Text='<%#Eval("Ensemble2_fee") %>' />
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a61" runat="server" Text='<%#Eval("Ensemble2_fee") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Ensemble3 Fee" ControlStyle-Width="80px" >
         <EditItemTemplate>
        <asp:TextBox ID="a7" runat="server" Text='<%#Eval("Ensemble3_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a71" runat="server" Text='<%#Eval("Ensemble3_fee") %>'/>
          </ItemTemplate>
          </asp:TemplateField>

            <asp:TemplateField HeaderText="StarzTroop Eisteddfod Fee"  >
         <EditItemTemplate>
        <asp:TextBox ID="a8" runat="server" Text='<%#Eval("Straztroop_Eisteddfod_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a81" runat="server" Text='<%#Eval("Straztroop_Eisteddfod_fee") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
            <asp:TemplateField HeaderText="StrazTroop Workshops Fee" ControlStyle-Width="80px">
         <EditItemTemplate>
        <asp:TextBox ID="a9" runat="server" Text='<%#Eval("Straztroop_4Workshops_per_year_fee") %>'/>
        </EditItemTemplate>
         <ItemTemplate>
         <asp:Label ID="a91" runat="server" Text='<%#Eval("Straztroop_4Workshops_per_year_fee") %>'/>
          </ItemTemplate>
          </asp:TemplateField>


         <asp:TemplateField>
        <EditItemTemplate>
        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/img/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/img/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
        </EditItemTemplate>
        <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/img/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/img/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" OnClientClick="return confirm('Are You Sure To Delete This?');"/>

        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
            </asp:GridView>

第二次gridview

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowcancelingedit="GridView1_RowCancelingEdit" DataKeyNames="Class_ID" 
    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" AllowPaging="true" PageSize="5" OnPageIndexChanging="gvDetails_PageIndexChanging"
    onrowupdating="GridView1_RowUpdating" CssClass="grid">
        <Columns>

     <asp:TemplateField HeaderText="Class ID" Visible="true">
     <ItemTemplate>
     <asp:Label ID="a1" runat="server" Text='<%#Eval("class_id") %>' />
      </ItemTemplate>
     </asp:TemplateField>   
     <asp:TemplateField HeaderText="Total_fee" ControlStyle-Width="100px" >
     <EditItemTemplate>
    <asp:TextBox ID="a2" runat="server" Text='<%#Eval("Total_fee") %>'/>
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a21" runat="server" Text='<%#Eval("Total_fee") %>'/>
      </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Weekly_fee"  >
     <EditItemTemplate>
    <asp:TextBox ID="a3" runat="server" Text='<%#Eval("Weekly_fee") %>'/>
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a31" runat="server" Text='<%#Eval("Weekly_fee") %>'/>
      </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Fort_night_fee" ControlStyle-Width="80px">
     <EditItemTemplate>
    <asp:TextBox ID="a4" runat="server" Text='<%#Eval("Fort_night_fee") %>'/>
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a41" runat="server" Text='<%#Eval("Fort_night_fee") %>'/>
      </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Monthly_fee" ControlStyle-Width="100px">
     <EditItemTemplate>
    <asp:TextBox ID="a5" runat="server" Text='<%#Eval("Monthly_fee") %>' />
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a51" runat="server" Text='<%#Eval("Monthly_fee") %>'/>
      </ItemTemplate>
      </asp:TemplateField>
     <asp:TemplateField HeaderText="Term_fee"  ControlStyle-Width="100px">
     <EditItemTemplate>
    <asp:TextBox ID="a6" runat="server" Text='<%#Eval("Term_fee") %>' />
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a61" runat="server" Text='<%#Eval("Term_fee") %>'/>
      </ItemTemplate>
      </asp:TemplateField>
     <asp:TemplateField HeaderText="Classes_per_week" ControlStyle-Width="80px" >
     <EditItemTemplate>
    <asp:TextBox ID="a7" runat="server" Text='<%#Eval("Classes_per_week") %>'/>
    </EditItemTemplate>
     <ItemTemplate>
     <asp:Label ID="a71" runat="server" Text='<%#Eval("Classes_per_week") %>'/>
      </ItemTemplate>
      </asp:TemplateField>
     <asp:TemplateField>
    <EditItemTemplate>
    <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/img/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
    <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/img/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/img/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
    <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/img/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" OnClientClick="return confirm('Are You Sure To Delete This?');"/>

    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
        </asp:GridView>

这是它背后的C#代码

private int classid;
    private bool recordexist=false;
    private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EnrolmentSystem"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            Binddropdown3();

            Loaddata();
        }
    }
    private void Binddropdown3()
    {
        try
        {
            con.Open();
            SqlCommand cmd3 = new SqlCommand("select * from Tbl_Class  order by Class_ID asc ", con);
            SqlDataReader dr = cmd3.ExecuteReader();
            DropDownList3.DataSource = dr;
            DropDownList3.Items.Clear();
            DropDownList3.Items.Add("--Select Class--");
            DropDownList3.DataTextField = "Class_Name";
            DropDownList3.DataValueField = "Class_ID";
            DropDownList3.DataBind();
            con.Close();
        }
        catch (Exception e)
        {
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        con.Open();

        string s = DropDownList3.SelectedItem.ToString();
        SqlCommand cmmd1 = new SqlCommand("select Tbl_Class.Class_ID from Tbl_Class where Tbl_Class.Class_Name='" + DropDownList3.SelectedItem + "'", con);
        SqlDataReader reader1 = cmmd1.ExecuteReader();
        if (reader1.HasRows)
        {
            while (reader1.Read())
            {
                classid = reader1.GetInt32(0);
            }
            reader1.Close();


            SqlCommand cmmmd1 = new SqlCommand("select Class_id from Tbl_Class_fee where Class_id='" +classid+ "'", con);
            SqlDataReader r1 = cmmmd1.ExecuteReader();

            if (r1.HasRows)
            {
                recordexist = true;
            }
            else 
            {
                recordexist=false;
            }

            r1.Close();

            if (recordexist == true)
            {
                SqlCommand cmd6 = new SqlCommand("Insert into Tbl_Fee_payment_packges values('" + classid + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList4.SelectedItem.Text + "')", con);
                cmd6.ExecuteNonQuery();

                con.Close();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = "Basic Fee of this Class is already Entered.You just need to enter the package detail of this class";
                con.Close();
            }
            else
            {

                SqlCommand cmd = new SqlCommand("Insert into Tbl_Fee_payment_packges values('" + classid + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList4.SelectedItem.Text + "')", con);
                cmd.ExecuteNonQuery();

                SqlCommand cmmd = new SqlCommand("Insert into Tbl_Class_fee values('" + classid + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox10.Text + "','" + null + "','" + null + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "')", con);
                cmmd.ExecuteNonQuery();


                con.Close();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = "Your Information Saved Successfully";
                con.Close();
            }
        }
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    protected void Loaddata()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand
            ("SELECT dbo.Tbl_Class_fee.*, dbo.Tbl_Class.* FROM dbo.Tbl_Class,Tbl_Class_fee where Tbl_Class.Class_ID = Tbl_Class_fee.Class_id ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();

        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
        con.Close();
    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        Loaddata();
    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        TextBox a1 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a2");
        TextBox a2 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a3");
        TextBox a3 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a4");
        TextBox a4 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a5");
        TextBox a5 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a6");
        TextBox a6 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a7");
        TextBox a7 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a8");
        TextBox a8 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a9");
        TextBox a9 = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("a10");


        con.Open();
        SqlCommand cmd = new SqlCommand("update Tbl_Class_fee set Enrolment_fee='" + a1.Text + "',Costume_fee='" + a2.Text + "',DSA_Academy_fee='" + a3.Text + "',Ensemble1_fee='" + a4.Text + "',Ensemble2_fee='" + a5.Text + "',Ensemble3_fee='" + a6.Text + "',Straztroop_Eisteddfod_fee='" + a7.Text + "',Straztroop_4Workshops_per_year_fee='" + a8.Text +  "' where Class_ID=" + userid, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = " Details Updated successfully";
        gvDetails.EditIndex = -1;
        Loaddata();
    }
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        Loaddata();
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        int  ProgramDescription = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());

        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Tbl_Class_fee where Class_id=" + userid, con);

        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Loaddata();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = ProgramDescription + " details deleted successfully";
        }
    }

    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvDetails.PageIndex = e.NewPageIndex;
            Loaddata();
        }
        catch { }
    }





    protected void selectrow(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        classid=Convert.ToInt32(btn.Text);
        string CommandName = btn.CommandName;
        string CommandArgument = btn.CommandArgument;
        Loaddata1();
    }


    protected void Loaddata1()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand
            ("SELECT dbo.Tbl_Fee_payment_packges.* FROM Tbl_Fee_payment_packges where Tbl_Fee_payment_packges.class_id='" + classid + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;

            GridView1.DataBind();
            GridView1.Visible = true;
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Loaddata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        TextBox a1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a2");
        TextBox a2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a3");
        TextBox a3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a4");
        TextBox a4 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a5");
        TextBox a5 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a6");
        TextBox a6 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a7");
        TextBox a7 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a8");
        TextBox a8 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a9");
        TextBox a9 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("a10");


        con.Open();
        SqlCommand cmd = new SqlCommand("update Tbl_Class_fee set Enrolment_fee='" + a1.Text + "',Costume_fee='" + a2.Text + "',DSA_Academy_fee='" + a3.Text + "',Ensemble1_fee='" + a4.Text + "',Ensemble2_fee='" + a5.Text + "',Ensemble3_fee='" + a6.Text + "',Straztroop_Eisteddfod_fee='" + a7.Text + "',Straztroop_4Workshops_per_year_fee='" + a8.Text + "' where Class_ID=" + userid, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = " Details Updated successfully";
        gvDetails.EditIndex = -1;
        Loaddata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Loaddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        int ProgramDescription = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Tbl_Class_fee where Class_id=" + userid, con);

        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Loaddata();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = ProgramDescription + " details deleted successfully";
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            Loaddata();
        }
        catch { }
    }

1 个答案:

答案 0 :(得分:0)

Change the LinkButton details as below

    <asp:LinkButton ID="LinkButton1" runat="server"  CommandName="bind"     CommandArgument = "<%#Eval("Class_id") %>" Text='<%#Eval("Class_id") %>'> </asp:LinkButton> 

并在代码中

protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
  {
     if (e.CommandName == "bind")
     {
       Loaddata1(); 
     }

  }

希望这个帮助