C# 如何使用 GridView 制作 foreach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17321215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How a can make foreach with GridView
提问by CaioVJesus89
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?
我有一个带有数据库信息的 GridView。在我的 aspx 中,我有 2 个复选框。我需要根据 AccessType 值为每一行检查一个复选框。如何在 foreach 中捕获 AccessType 值?
My aspx (GridView)
我的 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
我的.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;
//}
}
}
}
}
采纳答案by Tim Schmelter
You can access BoundFieldsvia e.Row.Cells[index].Text:
您可以BoundFields通过e.Row.Cells[index].Text以下方式访问:
foreach (GridViewRow row in GridView.Rows)
{
string accessType = row.Cells[3].Text;
}
However, I would use RowDataBoundinstead of an additional foreach.
但是,我会使用RowDataBound而不是额外的foreach.
Here is the RowDataBoundevent which is raised for every row in the GridViewwhen it was databound. Assuming the DataSourceis something like a DataTable:
这是在数据绑定时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;
}
}
回答by Foub
You could add the AccessType value to a HiddenField within the grid:
您可以将 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>
and then use the RowDataBound event handler to get the AccessType value from the HiddenField control and set the checkbox accordingly:
然后使用 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;
}
}
}
Hope this helps.
希望这可以帮助。

