如何使用GridView制作foreach

时间:2013-06-26 13:20:53

标签: c# asp.net .net

我有一个带有DB信息的GridView。 在我的aspx中,我有2个复选框。 我需要根据AccessType值在每个行的一个复选框中进行检查。 我如何在foreach中捕获AccessType值?

我的aspx(GridView)

<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="false" GridLines="None">
                <Columns>
                    <asp:BoundField DataField="AccessGroup" HeaderText="Access Group" />
                    <asp:BoundField DataField="FolderAccess" HeaderText="Folder Access" />
                    <asp:HyperLinkField DataNavigateUrlFields="group_manager" DataNavigateUrlFormatString="groupinfo.aspx?group={0}"
                        DataTextField="group_manager" HeaderText="Group Manager" />
                    <asp:BoundField DataField="AccessType" Visible="false" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" Text="Access to Read" />
                            <asp:CheckBox ID="CheckBox2" runat="server" Text="Access to Modify" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

我的.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var listaProdutos = new RequestAccess().ConsultarProdutos();
        if (listaProdutos != null)
        {
            this.GridView.DataSource = listaProdutos;
            this.GridView.DataBind();

            foreach (GridViewRow row in GridView.Rows)
            {
                CheckBox check = (CheckBox)row.FindControl("CheckBox1");
                CheckBox check2 = (CheckBox)row.FindControl("CheckBox2");


                //EX!!!
                //IF AccessType = 1
                //{
                //    check.Checked = true;
                //}

                //IF AccessType = 2
                //{
                //    check2.Checked = true;
                //}
            }
        }
    }
}

2 个答案:

答案 0 :(得分:15)

您可以通过BoundFields访问e.Row.Cells[index].Text

foreach (GridViewRow row in GridView.Rows)
{
    string accessType = row.Cells[3].Text;
}

但是,我会使用RowDataBound而不是额外的foreach

这是RowDataBound事件,它在数据绑定时为GridView中的每一行引发。假设DataSource类似于DataTable

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox check = (CheckBox)e.Row.FindControl("CheckBox1");
        CheckBox check2 = (CheckBox)e.Row.FindControl("CheckBox2");
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        int accesType = row.Field<int>("AccessType");
        check.Checked = accesType == 1;
        check2.Checked = accesType == 2;
    }
}

答案 1 :(得分:1)

您可以将AccessType值添加到网格中的HiddenField:

<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="false"
        onrowdatabound="GridView_RowDataBound">
        <Columns>
            <asp:BoundField DataField="AccessGroup" HeaderText="Access Group" />
            <asp:BoundField DataField="FolderAccess" HeaderText="Folder Access" />
            <asp:HyperLinkField DataNavigateUrlFields="group_manager" DataNavigateUrlFormatString="groupinfo.aspx?group={0}"
                    DataTextField="group_manager" HeaderText="Group Manager" />
            <asp:BoundField DataField="AccessType" Visible="false" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField ID="hdnAccessType" runat="server" Value='<%# Eval("AccessType") %>' />
                    <asp:CheckBox ID="chkReadOnly" runat="server" Enabled="false" />
                    <asp:CheckBox ID="chkModify" runat="server" Enabled="false" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

然后使用RowDataBound事件处理程序从HiddenField控件获取AccessType值并相应地设置复选框:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField hdnAccessType = (HiddenField)e.Row.FindControl("hdnAccessType");
            int accessType = int.Parse(hdnAccessType.Value.ToString());
            CheckBox chkReadOnly = (CheckBox)e.Row.FindControl("chkReadOnly");
            CheckBox chkModify = (CheckBox)e.Row.FindControl("chkModify");

            switch (accessType)
            {
                case 1:
                    chkReadOnly.Checked = true;
                    break;
                case 2:
                    chkModify.Checked = true;
                    break;
            }
        }
    }

希望这有帮助。