【问题标题】:Get bit value from database and show in CheckBox in GridView从数据库中获取位值并显示在 GridView 的 CheckBox 中
【发布时间】:2016-07-27 05:29:17
【问题描述】:

如何从数据库表中获取位值并在 GridView 中的复选框中显示其值(例如,如果值为 TRUE,则选中,否则未选中复选框)?另外,如何获取复选框值并将其插入具有“位”数据类型列的数据库表中?

这是我的桌子image

这是我的 GridView image

这是我的 .CS 代码:

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-left: 140px" />

<asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server" 
    style="margin-left: 161px" />

<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”。请告诉我如何解决这个问题。

【问题讨论】:

  • 代替这一行sql.InsertParameters["null"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("nulll")).Text;,你可以使用checked这样的属性-sql.InsertParameters["use_properColumn"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("nulll")).Checked;
  • 但是我在 中定义了名称为“null”的字符串,“null”只是字符串名称,字符串名称可以是任何东西,我认为这不是问题
  • 这里定义
  • @Krishnraj 值来自“insertparameters”
  • 好的,您是否尝试过添加这一行 - sql.InsertParameters["null"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("nulll")).Checked; ?调试应用程序,看看你会得到什么。

标签: c# asp.net gridview checkbox


【解决方案1】:

试试这条线-

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";

根据您的评论,在 gridview 输出中保持不变,两列复选框都取消勾选。

试试下面的代码

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

不需要这个代码-

Checked='<%# DataBinder.Eval(Container.DataItem,"Allow_Null_").ToString().Equals("1") %>'

【讨论】:

  • 很抱歉,但是当我尝试这个 'Checked='' enabled="false" />" ' 时会出现错误,这是错误 Specified cast is not valid. 描述:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误及其源自代码的位置的更多信息。异常详细信息:System.InvalidCastException:指定的强制转换无效。跨度>
  • @Saira:哦,我的错……请检查我更新的答案。您需要为此使用Convert.ToBoolean 方法。
  • 又出现错误,,,这里是错误“对象不能从 DBNull 转换为其他类型。这一行有错误” "
  • @KrishnaRaj 感谢它现在正在工作,当我从数据库中截断表时,现在我插入新值现在工作正常,问题来自表中的 NULL 值,Thnakss awesomeee
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多