gpt4 book ai didi

c# - 从数据库中获取位值并显示在 GridView 的 CheckBox 中

转载 作者:行者123 更新时间:2023-11-30 17:38:39 29 4
gpt4 key购买 nike

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

这是我的 table 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")).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") %>'

关于c# - 从数据库中获取位值并显示在 GridView 的 CheckBox 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457441/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com