将列表绑定到GridView并更新GridView

时间:2014-02-14 11:45:21

标签: asp.net c#-4.0

在我的页面中,我使用List绑定GridView。我的页面上有不同的字段,并将这些字段值存储到列表中并将其与Grid绑定。

它运行良好,但现在我想使用gridview中的复选框更新Gridview,点击复选框,该行中的关联值出现在不同的字段上,并且按钮保存点击gridview应该更新。

不确定如何去做。

我的代码示例如下:

aspx

<table class="tblPopUp">
    <tr>
        <td>
            <asp:Label ID="lblCaste" CssClass="Label" runat="server" Text="Caste"></asp:Label>
        </td>
        <td>
            <asp:DropDownList ID="ddlCaste" runat="server" CssClass="Dropdownlist myddlinPopUp"></asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblNo_OfPosition" CssClass="Label" runat="server" Text="No Of Position"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtNoOfPositioninPopUp" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblMinAge" CssClass="Label" runat="server" Text="Min Age"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtMinAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblMaxAge" CssClass="Label" runat="server" Text="Max Age"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtMaxAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
        </td>
    </tr>
    <tr>                    
        <td>
            <asp:Button ID="btnSaveInPopUpReservation" runat="server" CssClass="button" Text="Save" OnClick="btnSaveInPopUpReservation_Click" />
        </td>
    </tr>
</table>
<asp:GridView ID="gridReservationDetails" runat="server" GridLines="Both" AutoGenerateColumns="false" CssClass="gridview" ShowHeaderWhenEmpty="true">
    <Columns>
        <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
            <HeaderTemplate>
                <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
            </ItemTemplate>
            <HeaderStyle Font-Bold="False" />
            <ItemStyle Font-Bold="False" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Sl. No" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="item">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Caste" DataField="casteName" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="No Of Position" DataField="NoOfPosition" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="Min Age" DataField="MinAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="Max Age" DataField="MaxAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
    </Columns>
</asp:GridView>

aspx.cs

public class CommonHRMS_PositionRequisitionReservationDetails
{
    public int casteID { get; set; }
    public string casteName { get; set; }
    public int NoOfPosition { get; set; }
    public int MaxAge { get; set; }
    public int MinAge { get; set; }
}

protected List<CommonHRMS_PositionRequisitionReservationDetails> FillReservation()
{
    List<CommonHRMS_PositionRequisitionReservationDetails> lst = new List<CommonHRMS_PositionRequisitionReservationDetails>();
    List<CommonHRMS_PositionRequisitionReservationDetails> lstPrevious = new List<CommonHRMS_PositionRequisitionReservationDetails>();
    lstPrevious = (List<CommonHRMS_PositionRequisitionReservationDetails>)Session["cprrd"];
    if (lstPrevious != null)
    {
        CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails();
        obj.casteID = int.Parse(ddlCaste.SelectedValue);
        obj.casteName = ddlCaste.SelectedItem.ToString();
        obj.NoOfPosition = int.Parse(txtNoOfPositioninPopUp.Text);
        obj.MaxAge = int.Parse(txtMaxAge.Text);
        obj.MinAge = int.Parse(txtMinAge.Text);
        lstPrevious.Add(obj);
        Session["cprrd"] = lstPrevious;
        return lstPrevious;
    }
    else
    {
        CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails();
        obj.casteID = int.Parse(ddlCaste.SelectedValue);
        obj.casteName = ddlCaste.SelectedItem.ToString();
        obj.NoOfPosition = Convert.ToInt32(txtNoOfPositioninPopUp.Text);
        obj.MaxAge = Convert.ToInt32(txtMaxAge.Text);
        obj.MinAge = Convert.ToInt32(txtMinAge.Text);
        lst.Add(obj);
        Session["cprrd"] = lst;
        return lst;
    }
}


protected void btnSaveInPopUpReservation_Click(object sender, EventArgs e)
{
    List<CommonHRMS_PositionRequisitionReservationDetails> lst = FillReservation();
    gridReservationDetails.DataSource = lst;
    gridReservationDetails.DataBind();        
}

1 个答案:

答案 0 :(得分:0)

试试此代码

foreach (GridViewRow row in yourGridViewID.Rows)

{
    CheckBox check = (CheckBox)row.FindControl("CheckBoxName");

    if (check.Checked)
    {
        //Button Event Here
    }
    else
    {
        //Else Part..!
    }
}