How to make foreach with a gridview

I have a GridView with DB information. In my aspx, I have 2 checkbox. I need to do a check in one checkbox for each row according to the AccessType value. How can I capture the AccessType value in foreach?

My 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> 

My.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; //} } } } } 
+7
source share
2 answers

You can access BoundFields through e.Row.Cells[index].Text :

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

However, I would use a RowDataBound instead of an additional foreach .

Here is the RowDataBound event that is RowDataBound for each row in the GridView when it was bound to the database. Assuming the DataSource is something like a 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; } } 
+12
source

You can add the AccessType value to the HiddenField in the grid:

 <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> 

and then use the RowDataBound event handler to get the AccessType value from the HiddenField element and check the box accordingly:

 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; } } } 

Hope this helps.

+1
source

All Articles