数据库系统之1.使用gridview动态增加行的实现
数据库系统之1.使用gridview动态增加行的实现
现在在实习,项目经理要求做个简单的数据库系统,实现简单的创建数据库,删除数据库,编辑字段等功能
今天简单做了一些,首先是使用gridview动态增加行的实现
后台实现代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTestData();
}
}
protected void BindTestData()
{
//模拟出一些原始数据绑定DataGrid
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("字段名称");
dt1.Columns.Add("字段长度");
dt1.Columns.Add("中文注释");
dt1.Columns.Add("默认值");
dt1.Columns.Add("字段类型");
dt1.Columns.Add("是否为空");
dt1.Columns.Add("是否自增");
dt1.Rows.Add(new object[] { 1, "Id", "4", "主键", "0","int","否","是" });
dt1.Rows.Add(new object[] { 2, "adder", "4", "添加人员", "0", "int", "否", "否" });
dt1.Rows.Add(new object[] { 3, "addtime", "", "添加时间", "getdate()", "datetime", "否", "否" });
dt1.Rows.Add(new object[] { 4, "moder", "4", "修改人员", "0", "int", "否", "否" });
dt1.Rows.Add(new object[] { 5, "modtime", "", "修改时间", "getdate()", "datetime", "否", "否" });
dt1.Rows.Add(new object[] { 6, "delstatus", "1", "删除标识", "0", "tinyint", "否", "否" });
dt1.AcceptChanges();
this.GridView1.DataSource = dt1;
this.GridView1.DataBind();
}
protected DataTable GetDataFromGrid()
{
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("字段名称");
dt1.Columns.Add("字段长度");
dt1.Columns.Add("中文注释");
dt1.Columns.Add("默认值");
dt1.Columns.Add("字段类型");
dt1.Columns.Add("是否为空");
dt1.Columns.Add("是否自增");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] = GridView1.DataKeys[i].Value;
newRow[1] = ((TextBox)gRow.FindControl("TextBox1")).Text;
newRow[2] = ((TextBox)gRow.FindControl("TextBox2")).Text;
newRow[3] = ((TextBox)gRow.FindControl("TextBox3")).Text;
newRow[4] = ((TextBox)gRow.FindControl("TextBox4")).Text;
newRow[5] = ((DropDownList)gRow.FindControl("DropDownList1")).SelectedValue;
newRow[6] = ((DropDownList)gRow.FindControl("DropDownList2")).SelectedValue;
newRow[7] = ((DropDownList)gRow.FindControl("DropDownList3")).SelectedValue;
dt1.Rows.Add(newRow);
}
dt1.AcceptChanges();
return dt1;
}
protected void btn_addfile_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
newRow["字段类型"] = "int";
newRow["是否为空"] = "否";
newRow["是否自增"] = "否";
newRow["默认值"] = "";
newRow["中文注释"] = "";
dt.Rows.Add(newRow);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
前台页面代码:
<center>
需要创建的数据库表名:
<asp:TextBox ID="txt_tbname" runat="server" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="txt_tbname"
Display="Dynamic" ErrorMessage="数据库表名不能为空!"></asp:RequiredFieldValidator>主键名称:
<asp:TextBox ID="txt_pk" runat="server" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv2" runat="server" ControlToValidate="txt_pk"
Display="Dynamic" ErrorMessage="请明确主键名称!"></asp:RequiredFieldValidator><br />
<asp:GridView ID="GridView1" CssClass="dg_perant" runat="server" AutoGenerateColumns="False"
DataKeyNames="id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="字段名称">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "字段名称")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="字段长度">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "字段长度")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="中文注释">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "中文注释")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="默认值">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "默认值")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="字段类型">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "字段类型")%>'
DataSourceID="ObjectDataSource1" DataTextField="name" DataValueField="name"
>
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetDataSet" TypeName="SqlHelper">
<SelectParameters>
<asp:Parameter Name="safeSql" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="是否为空">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "是否为空")%>'>
<asp:ListItem Value="否" Selected="True">否</asp:ListItem>
<asp:ListItem Value="是">是</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="是否自增">
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "是否自增")%>'>
<asp:ListItem Value="否" Selected="True">否</asp:ListItem>
<asp:ListItem Value="是">是</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btn_addfile" runat="server" class="Boutton_Orange" Text="添加空行"
onclick="btn_addfile_Click" />
<asp:Button ID="btn_save" runat="server" class="Boutton_Orange" Text="保存所有"
onclick="btn_save_Click" />
<asp:Button ID="btn_del" runat="server" class="Boutton_Orange" Text="删除数据库"
onclick="btn_del_Click" />
</center>
原文出自艾新草电脑论坛
数据库系统之1.使用gridview动态增加行的实现

浙公网安备 33010602011771号