我正在一个项目上工作。我有一个gridview有一列有checkbox.Now我想用数据填充我的GridView并根据我的状态字段检查/取消选中相应的复选框。直到现在我正在处理gridview行数据绑定event.But,但我只想知道是否有任何方法来标记复选框,而绑定源。检查/取消选中GridView绑定源中的复选框
查询我正在执行以获得gridview的数据
select s.studentcode,s.studentname,r.status from tblstudent s join tblresults r on s.studentcode=r.studentcode
输出我得到
现在我想,如果我的状态是“促进”复选框接受检查,如果是'不推广'复选框得到unchecked.I不想要使用行数据绑定方法。只想要我的工作在下面的行完成
sql = "select s.studentcode,s.studentname,r.status from tblstudent s join tblresults r on s.studentcode=r.studentcode";
ds = obj.openDataset(sql, Session["SchoolCode"].ToString());
if (ds.Tables[0].Rows.Count > 0)
{
grdstudents.DataSource = ds;
grdstudents.DataBind();
grdstudents.Visible = true;
}
else
{
alertdiv.Visible = true;
lblalertmsg.Visible = true;
btnclose.Visible = true;
lblalertmsg.Text = "No Record found";
}
在aspx页面里面的GridView
解决方案
查询
select s.studentcode,s.studentname,if(r.status='Promoted',true,false) as status from tblstudent s left join tblresults r on s.studentcode=r.studentcode where s.classcode='10000'
grdstudents.DataSource = ds;// note we binded to final table
grdstudents.DataBind();
复选框字段:
<ItemTemplate>
<asp:CheckBox ID="chkapply" runat="server" Checked='<%# Convert.ToBoolean(Eval("status"))%>'/>
</ItemTemplate>
该解决方案将帮助您避免额外的代码,以便在使用itembinding或行的数据绑定写入gridview事件
但是如何将复选框绑定到状态字段 ItemTemplate> –
rupinder18
您应该使用“Checked”属性:'runat =“server”/> ItemTemplate>或者试试看:Checked ='<%#Convert.ToBoolean Eval(“Column_Name_Here”))%>' –
ya it worked thanx – rupinder18