我尝试了很多,但我无法找到解决这个问题的方法。这就是我的问题。实际上,我正在创建" DATABASE EDITOR"在ASP.NET C#中。这是我的数据库" tbl_field"表
其中用户插入值并创建表。我知道这可以轻松完成查询,我可以做到,但首先我想在表中存储值。我希望当用户点击创建表然后每次这样gridview打开,这里是图像,当用户点击"插入"然后将值插入数据库表" tbl_field"并且还显示上面插入的行。但问题是当另一个用户登录然后创建表格然后再次显示这个griview和插入的行。我是初学者,我不知道如何处理这个问题。我&#39 ;我试图解释我的问题,希望我的问题很清楚。这是我的aspx代码:`
<label id="welcome" style="color: Blue" runat="server">
</label>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="user" runat="Server">
<label style="position:relative; top:60px; left:140px runat="server">Table Name </label>
<asp:TextBox ID="txttable" runat="server" style="position:relative; top:60px; left:140px; border-style:double;" ></asp:TextBox>
<asp:GridView ID="GridView2" runat="server" ShowHeaderWhenEmpty="True"
AutoGenerateColumns="False" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2"
ShowFooter="True" Style="margin-left: 136px; margin-top: 72px;
margin-right: 0px;" Height="16px" Width="685px" onselectedindexchanged="GridView2_SelectedIndexChanged" >
<Columns>
<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="textColname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="INSERT" Text="*" ID="insertcolname" ControlToValidate="textColname"
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='<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"Allow_Null_")) %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"Allow_Null_"))%>' 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='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"Primary_Key_")) %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server"
Checked='<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"Primary_Key_")) %>' Enabled="false" />
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="primary" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Insert">
<FooterTemplate>
<asp:LinkButton ValidationGroup="INSERT" ID="LinkButton1" runat="server" OnClick="lbinssert">INSERT</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</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="25px" style="margin-left: 140px" />
<br />
<input type="submit" style="position:relative; width:30px; left:452px; top: 0px; width: 86px;"
value="Create table" name="create"/>
<asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server"
style="margin-left: 142px" />
<asp:SqlDataSource ID="sql" runat="server"
ConnectionString="<%$ ConnectionStrings:db_compilerConnectionString %>"
SelectCommand="SELECT [Table_Name], [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] ([Table_Name],[Column_Name],[Data_Type],[Database_id],[Allow_Null_],[Primary_Key_])Values (@TableName,@ColumnName,@Dtype,@id,@null,@primary)"
UpdateCommand="UPDATE [tbl_field] SET [Table_Name]=@TableName, [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="TableName" Type="String" />
<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="TableName" Type="String" />
<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>
这是aspx.cs代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_field", cnn);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
GridView2.DataSource = dt;
FixGridFooter(dt);
}
public void lbinssert(Object sender,EventArgs e)
{
sql.InsertParameters["TableName"].DefaultValue = txttable.Text;
sql.InsertParameters["ColumnName"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("textColname")).Text;
sql.InsertParameters["Dtype"].DefaultValue = ((DropDownList)GridView2.FooterRow.FindControl("DropDownList2")).Text;
sql.InsertParameters["id"].DefaultValue = Session["value"].ToString();
sql.InsertParameters["null"].DefaultValue =
((CheckBox)GridView2.FooterRow.FindControl("nulll")).Checked == true ? "true" : "false";
sql.InsertParameters["primary"].DefaultValue = ((CheckBox)GridView2.FooterRow.FindControl("primary")).Checked == true ? "true" : "false";
sql.Insert();
}
public void FixGridFooter(DataTable dataSour)
{
dataSour.Rows.Add(dataSour.NewRow());
dataSour.Rows[0]["Table_Name"] = 0;
dataSour.Rows[0]["Column_Name"] = 0;
dataSour.Rows[0]["Data_Type"] = "";
dataSour.Rows[0]["Database_id"] = 0;
dataSour.Rows[0]["Allow_Null_"] = true;
dataSour.Rows[0]["Primary_Key_"] = false;
dataSour.Rows[0]["Date"] = DateTime.Now.ToString("dd MMM yyyy");
GridView2.DataSource = dataSour;
GridView2.DataBind();
//hide empty row - if you want to display it on an event like a button click else it will display as per default.
GridView2.Rows[0].Visible = false;
GridView2.ShowFooter = true;
}
实际上,我绑定了数据库表中的值,因此每次表都显示插入值等。
答案 0 :(得分:0)
如果您的问题是最初只显示标题,则在未插入数据时,您可以尝试使用GridView
属性ShowHeaderWhenEmpty
。此属性指示当列没有数据时,GridView控件中列的标题是否可见。
如果在gridview中没有数据时需要显示空行和标题,则需要将gridview
与包含一个虚拟元素的List绑定,可以在no时初始化数据来自数据库。