问题描述
我有一个带有复选框字段和几个绑定字段的 gridview 控件.复选框字段不直接映射到数据库中的字段.相反,我想从数据库中的一个字段中读取一个值并选中"一些复选框.
例如,给定以下来自数据库的数据 -> 数据表
已处理姓名日期Y 米老鼠 11/15/2011N 唐老鸭 4/01/2012Y 詹姆斯邦德 5/02/2011我希望 gridview 显示一个复选框并将框的值设置为 UNCHECKED,其中 PROCESSED = N 和 PROCESSED = Y 要么有一个不可编辑的复选框,要么根本没有复选框.
已处理姓名日期[/] 米老鼠 11/15/2011[ ] 唐老鸭 4/01/2012[/] 詹姆斯邦德 5/02/2011为了填充 gridview,对数据库运行 SQL stmt,并将 SQL 查询的结果存储在数据表中.在将数据表绑定到 gridview 之前,我想检查已处理"字段并根据该值设置复选框.
这是 gridview 控件(为清楚起见缩短):
<asp:GridView ID="gridview_all_applicants" runat="server" AllowPaging="True"><asp:TemplateField HeaderText="Complete"><项目模板><asp:CheckBox ID="process_flag" runat="server"/></ItemTemplate></asp:TemplateField><asp:BoundField DataField="lastname" HeaderText="Last Name" ReadOnly="True" SortExpression="lastname"/>
这是我到目前为止在背后的代码中的内容
SqlCommand cmd = new SqlCommand(sql 查询这里);SqlDataAdapter da = new SqlDataAdapter();数据表 dt = 新数据表();da.SelectCommand = cmd;//将select语句的结果保存到数据表中da.Fill(dt);foreach(dt.Rows 中的 DataRow r){//对查询返回的数据做一些处理//从返回的数据中读取字符值并设置复选框procflag = r["process_flag"].ToString().ToLower();CheckBox chkbox = new CheckBox();if (procflag == null || procflag == "n"){//将复选框设置为未检查"}别的{//将复选框设置为已检查"并使其不可点击//----或者---- 根本不显示复选框.}}//结束每个gridview_all_applicants.DataSource = DT;gridview_all_applicants.DataBind();
非常感谢任何帮助.
你可以这样做:
sql server 中的第一个:
选择CAST(CASE PROCESSED WHEN 'Y' THEN 1 ELSE 0 END AS BIT) 已处理名称日期从示例表
在 C# 代码中:
SqlCommand cmd = new SqlCommand(这里是sql查询);SqlDataAdapter da = new SqlDataAdapter();数据表 dt = 新数据表();da.SelectCommand = cmd;//将select语句的结果保存到数据表中da.Fill(dt);gridview_all_applicants.DataSource = DT;gridview_all_applicants.DataBind();
最后在 aspx 中:
<项目模板><asp:CheckBox ID="process_flag" runat="server" Checked='<%# bool.Parse(Eval("PROCESSED").ToString()) %>'Enable='<%# !bool.Parse(Eval("PROCESSED").ToString()) %>'/></ItemTemplate>
I have a gridview control with a checkbox field and several bound fields. The checkbox field does not directly map to a field in the database. Rather, i want to read a value from a field in the database and "check" some of the checkboxes.
For example, given the following data from the database -> datatable
PROCESSED NAME DATE Y Mickey Mouse 11/15/2011 N Donald Duck 4/01/2012 Y James Bond 5/02/2011
I would like the gridview to display a checkbox and set the value of boxes to UNCHECKED where PROCESSED = N and for PROCESSED = Y either have an uneditable checkbox or no checkbox at all.
PROCESSED NAME DATE [/] Mickey Mouse 11/15/2011 [ ] Donald Duck 4/01/2012 [/] James Bond 5/02/2011
To populate the gridview, a SQL stmt is run against a database, and the result of the SQL query is stored in a datatable. Before binding the datatable to the gridview, i would like to check the "processed" field and set the checkbox based on that value.
Here is the gridview control (shortened for clarity):
<asp:GridView ID="gridview_all_applicants" runat="server" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="Complete">
<ItemTemplate>
<asp:CheckBox ID="process_flag" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="lastname" HeaderText="Last Name" ReadOnly="True" SortExpression="lastname" />
Here is what i have so far in the code behind
SqlCommand cmd = new SqlCommand(sql query here);
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
// Save results of select statement into a datatable
da.Fill(dt);
foreach (DataRow r in dt.Rows)
{
// do some processing of data returned from query
// read the char value from the returned data and set checkbox
procflag = r["process_flag"].ToString().ToLower();
CheckBox chkbox = new CheckBox();
if (procflag == null || procflag == "n")
{
// SET CHECKBOX TO "NOT CHECKED"
}
else
{
// SET CHECKBOX TO "CHECKED" AND MAKE IT UNCLICKABLE
// ----OR---- DO NOT DISPLAY CHECKBOX AT ALL.
}
} // end for each
gridview_all_applicants.DataSource = dt;
gridview_all_applicants.DataBind();
any help is greatly appreciated.
You can do it like this:
First in sql server:
SELECT
CAST(CASE PROCESSED WHEN 'Y' THEN 1 ELSE 0 END AS BIT) AS PROCESSED
NAME
DATE
FROM ExampleTable
in c# code:
SqlCommand cmd = new SqlCommand(sql query here);
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
// Save results of select statement into a datatable
da.Fill(dt);
gridview_all_applicants.DataSource = dt;
gridview_all_applicants.DataBind();
and finally in aspx:
<asp:TemplateField HeaderText="Complete">
<ItemTemplate>
<asp:CheckBox ID="process_flag" runat="server" Checked='<%# bool.Parse(Eval("PROCESSED").ToString()) %>' Enable='<%# !bool.Parse(Eval("PROCESSED").ToString()) %>'/>
</ItemTemplate>
这篇关于根据数据表值在gridview中设置复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!