在GridView的复选框,设置基于数据表值 [英] set checkbox in gridview based on datatable value
问题描述
我有一个复选框字段和几个绑定字段GridView控件。复选框域不直接映射到数据库中的一个字段。 。相反,我希望在数据库中读取一个字段的值,检查一些复选框
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
我想在GridView显示一个复选框,并提示框的值设置为UNCHECKED,其中PROCESSED = N和对加工= Y要么有一个不可编辑的复选框或没有复选框都没有。
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
要填充的GridView,一个SQL语句是对数据库运行,以及SQL查询的结果存储在一个DataTable。之前的数据表绑定到GridView,我想检查处理区域,并设置基于该值的复选框。
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.
下面是GridView控件(缩短清晰度):
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:
首先在SQL Server:
First in sql server:
SELECT
CAST(CASE PROCESSED WHEN 'Y' THEN 1 ELSE 0 END AS BIT) AS PROCESSED
NAME
DATE
FROM ExampleTable
在C#代码:
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();
终于在ASPX:
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的复选框,设置基于数据表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!