none
数据库中的bit类型在gridview中的显示 RRS feed

  • 问题

  • 我的数据库中有2bit类型的字段(Allow_Null_ Primary_Key_)对应gridview中的checkbox(如果值为true的值,checkbox选中,返回不选中) 我想做这样的新增和查询功能。

    这是我目前的代码:

    public void lbinssert(Object sender,EventArgs e)
    {
        sql.InsertParameters["ColumnName"].DefaultValue =
            ((TextBox)GridView1.FooterRow.FindControl("txtColName")).Text;
    
        sql.InsertParameters["Dtype"].DefaultValue =
            ((DropDownList)GridView1.FooterRow.FindControl("DropDownList2")).Text;
    
        sql.InsertParameters["id"].DefaultValue =
            ((DropDownList)GridView1.FooterRow.FindControl("DropDownList2")).Text;
    
        sql.InsertParameters["null"].DefaultValue =
            ((CheckBox)GridView1.FooterRow.FindControl("nulll")).Text;
    
        sql.InsertParameters["primary"].DefaultValue = 
            ((CheckBox)GridView1.FooterRow.FindControl("primary")).Text;
    
        sql.Insert();
    }
    

    ASPX

    <asp:TemplateField HeaderText="Column_Name" SortExpression="Column_Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" Text='<%#Bind("Column_Name")%>'></asp:TextBox>
                    <asp:RequiredFieldValidator Text="*" ID="colname" ControlToValidate="txtName" runat="server" ErrorMessage="Column Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column_Name") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtColName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ValidationGroup="INSERT" Text="*" ID="insertcolname" ControlToValidate="txtColName"runat="server" ErrorMessage="Column Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Data_Type" SortExpression="Data_Type">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Data_Type") %>'>
                        <asp:ListItem>Select Datatype</asp:ListItem>
                        <asp:ListItem>int</asp:ListItem>
                        <asp:ListItem>Float</asp:ListItem>
                        <asp:ListItem>Varchar(50)</asp:ListItem>
                        <asp:ListItem>Varchar(100)</asp:ListItem>
                        <asp:ListItem>double</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="datatype" ControlToValidate="DropDownList1"
                        runat="server" ErrorMessage="Data Type is Required" ForeColor="Red" InitialValue="Select Datatype">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Data_Type") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem>Select Datatype</asp:ListItem>
                        <asp:ListItem>int</asp:ListItem>
                        <asp:ListItem>Float</asp:ListItem>
                        <asp:ListItem>Varchar(50)</asp:ListItem>
                        <asp:ListItem>Varchar(100)</asp:ListItem>
                        <asp:ListItem>double</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ValidationGroup="INSERT" Text="*"  ID="datatype" ControlToValidate="DropDownList2"
                        runat="server" ErrorMessage="Data Type is Required" ForeColor="Red" InitialValue="Select Datatype">
                    </asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Allow_Null_" SortExpression="Allow_Null_">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"Allow_Null_").ToString().Equals("1") %>'/>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"Allow_Null_").ToString().Equals("1") %>' enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:CheckBox ID="nulll" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Primary_Key_" SortExpression="Primary_Key_">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"Primary_Key_").ToString().Equals("1") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" 
                        Checked='<%# DataBinder.Eval(Container.DataItem,"Primary_Key_").ToString().Equals("1") %>' Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:CheckBox ID="primary" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date" SortExpression="Date">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:LinkButton ValidationGroup="INSERT" OnClick="lbinssert" ID="lblInsert" runat="server">Insert</asp:LinkButton>
                </FooterTemplate> 
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>
    
    <asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red"
        runat="server" Height="54px" style="margin- />
    
    <asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server" 
        style="margin- />
    
    <asp:SqlDataSource ID="sql" runat="server" 
        ConnectionString="<%$ ConnectionStrings:db_compilerConnectionString %>"
        SelectCommand="SELECT [Column_Name], [Data_Type], [Allow_Null_], [Primary_Key_], [Date] FROM [tbl_field]"
        DeleteCommand="DELETE * FROM [tbl_field] WHERE [Field_id]=@field" InsertCommand="INSERT INTO [tbl_field] ([Column_Name],[Data_Type],[Database_id],[Allow_Null_],[Primary_Key_])Values (@ColumnName,@Dtype,@id,@null,@primary)"
        UpdateCommand="UPDATE [tbl_field] SET [Column_Name]=@ColumnName,[Data_Type]=@Dtype,[Database_id]=@id,[Allow_Null_]=@null,[Primary_Key_]=@primary">
        <DeleteParameters>
            <asp:Parameter Name="field" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ColumnName" Type="String" />
            <asp:Parameter Name="Dtype" Type="String" />
            <asp:Parameter Name="id" Type="String" />
            <asp:Parameter Name="null" Type="Boolean" />
            <asp:Parameter Name="primary" Type="Boolean" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="ColumnName" Type="String" />
            <asp:Parameter Name="Dtype" Type="String" />
            <asp:Parameter Name="id" Type="String" />
            <asp:Parameter Name="null" Type="Boolean"/>
            <asp:Parameter Name="primary" Type="Boolean" /> 
        </UpdateParameters>
    </asp:SqlDataSource>
    
    新增的时候Allow_Null_ Primary_Key_的值是NULL 帮忙看一下是什么原因?谢谢!

    2016年4月10日 7:42

答案

  • 你好,

    请试一个以下的代码,这样不会出现NULL的情况。

    sql.InsertParameters["null"].DefaultValue =
      ((CheckBox)GridView1.FooterRow.FindControl("nulll")).Checked == true ? "true":"false";
    
    sql.InsertParameters["primary"].DefaultValue = 
       ((CheckBox)GridView1.FooterRow.FindControl("primary")).Checked == true ? "true":"false";

    前端的代码显示这块,改成这样的。

    <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" 
                Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"Allow_Null_")) %>' enabled="false" />
        </ItemTemplate>
    希望能帮到你。

    Best regards,

    Cole Wu

    2016年4月10日 14:12