asp.net Gridview来自代码隐藏的布尔列 [英] asp.net Gridview Boolean column from code-behind
问题描述
我使用具有布尔字段的相同数据表将其绑定到2个不同的网格视图。一个gridview是在aspx文件中设计的,布尔字段有一个模板列
< asp:TemplateField>
< itemtemplate>
< asp:CheckBox ID =cbrunat =serverWidth =100px
Checked ='<%#Convert.ToBoolean(Eval(boolField))%>' />
< itemstyle horizontalalign =中心>
此工作正常
但第二个网格是在后面的代码中构建的
gr2.Columns.Clear();
i i = 0; i< dt.Rows.Count; i ++)
{
BoundField dF = new BoundField(); < br $>
dF.DataField = dt.Rows [i] .ItemArray [0] .ToString();
dF.HeaderText = dt.Rows [i] .ItemArray [1] .ToString();
gr2.Columns.Add(dF);
}
gr2.DataSource = dt;
gr2.DataBind();
this也有效,但显示s bool列值为True / False。
我要显示是/否或复选框。我知道如何通过后面的代码中的ItemTemplate实现Checkbox,但无法弄清楚如何填充复选框,换句话说如何完成Checked ='<%#Convert.ToBoolean(Eval(boolField))%> ;'在后面的代码中。
为了通过代码隐藏实现它,请找到以下步骤:
Step1 :在griview中添加OnRowDataBound属性。
< < span class =code-leadattribute> asp:gridview id = GridView1 runat = server autogeneratecolumns = False onrowdatabound = GridView1_RowDataBound cellpadding = 4 forecolor = #333333 gridlines = 无 xmlns:asp = #unknown >
< 列 >
< asp:boundfield datafield = UserId headertext = 用户ID / >
< asp:templatefield headertext = 选择 >
< itemtemplate >
< asp:checkbox id = chkSelect runat = server / >
< / itemtemplate >
< / asp:templatefield > < / columns >
< / asp:gridview>
Step2 :添加OnRowDataBound方法代码背后。
受保护 void GridView1_RowDataBound( object sender,GridViewRowEventArgs e)
{
// 检查行类型,该行类型应为数据行
if (e.Row.RowType == DataControlRowType .DataRow)
{
// 找到复选框并从数据源中分配值
((CheckBox)e.Row.FindControl( chkSelect) ).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[ 1 ]);
}
}
http://www.c-sharpcorner.com/uploadfile/MohanKumar.R/selecting-checkboxes-inside-the-gridview-control/ [ ^ ]
I using the same datatable that has a Boolean field to bind it to 2 different gridviews. One gridview is designed in aspx file and there is a template column for Boolean field
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="cb" runat="server" Width="100px"
Checked='<%# Convert.ToBoolean(Eval("boolField")) %>' />
<itemstyle horizontalalign="Center">
this works fine
but the second grid is built in the code behind
gr2.Columns.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
BoundField dF = new BoundField();
dF.DataField = dt.Rows[i].ItemArray[0].ToString();
dF.HeaderText = dt.Rows[i].ItemArray[1].ToString();
gr2.Columns.Add(dF);
}
gr2.DataSource = dt;
gr2.DataBind();
this also works but shows bool column values as True/False.
I'd like it either to display Yes/No or check boxes. I know how to implement Checkbox via ItemTemplate in the code behind but can't figure out how to populate check boxes, in other words how to accomplish Checked='<%# Convert.ToBoolean(Eval("boolField")) %>' in the code behind.
In order to implement it through codebehind please find below steps:
Step1: Add OnRowDataBound property in griview.
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" onrowdatabound="GridView1_RowDataBound" cellpadding="4" forecolor="#333333" gridlines="None" xmlns:asp="#unknown"> <columns> <asp:boundfield datafield="UserId" headertext="User Id" /> <asp:templatefield headertext="Select"> <itemtemplate> <asp:checkbox id="chkSelect" runat="server" /> </itemtemplate> </asp:templatefield></columns> </asp:gridview>
Step2: Add OnRowDataBound method in code behind.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //Check for the row type, which should be data row if (e.Row.RowType == DataControlRowType.DataRow) { //Find the check boxes and assign the values from the data source ((CheckBox)e.Row.FindControl("chkSelect")).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[1]); } }
http://www.c-sharpcorner.com/uploadfile/MohanKumar.R/selecting-checkboxes-inside-the-gridview-control/[^]
这篇关于asp.net Gridview来自代码隐藏的布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!