I have an GridView with DB informations. In my aspx, I have 2 checkbox. I need make check in one checkbox for each row according to AccessType value. How I can capture 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;
//}
}
}
}
}
You can access BoundFields via e.Row.Cells[index].Text:
foreach (GridViewRow row in GridView.Rows)
{
string accessType = row.Cells[3].Text;
}
However, I would use RowDataBound instead of an additional foreach.
Here is the RowDataBound event which is raised for every row in the GridView when it was databound. 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;
}
}
You could add the AccessType value to a HiddenField within 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 control and set the checkbox 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With