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 val
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>
How about a database agnostic solution, just on the off chance you are using a non sqlserver database ;)
<asp:CheckBox ID="process_flag_check" runat="server"
Checked='<%# Eval("process_flag").ToString() == "Y" ? "True": "False" %>'
Enabled="false"/>
</asp:Content>
<asp:CheckBox ID="process_flag_check" runat="server"
Checked='<%# bool.Parse(Eval("process_flag").ToString() == "Y" ? "True": "False") %>'
Enabled="false"/>
</asp:CheckBox>
This one Perfectly Works.I have modified using Kevin and marco Code. Thank u Kevinv and Marco