set checkbox in gridview based on datatable value

后端 未结 3 1955
名媛妹妹
名媛妹妹 2021-01-03 01:07

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

相关标签:
3条回答
  • 2021-01-03 01:49

    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>
    

    0 讨论(0)
  • 2021-01-03 02:02

    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>
    
    0 讨论(0)
  • 2021-01-03 02:04
    <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

    0 讨论(0)
提交回复
热议问题