繁体   English   中英

从下拉列表动态填充表名称并在Gridview中显示

[英]Dynamically populating table names from dropdownlist and displaying in Gridview

我正在创建一个包含一个Dropdownlist和Gridview的网页。

Query is Dropdownlist将包含SQL Server数据库表列表。 当我从下拉列表中选择一个表名时,Gridview需要显示整个表数据并能够执行编辑,更新,删除,取消操作。

当我单击编辑时,Gridview需要显示更新和取消按钮,并且它的更新应该更新下拉列表并删除。

我的代码看起来像这样:

HTML页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataGridView_Sample._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            font-weight: bold;
            text-decoration: underline;
            font-size: x-large;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <h5 class="style1">
            Data Grid View Sample</h5>

    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
         <asp:ListItem Text="-- Select --" Value=""></asp:ListItem>
        <asp:ListItem Text="Emp" Value="Emp"></asp:ListItem>
        <asp:ListItem Text="Dept" Value="Dept"></asp:ListItem>
    </asp:DropDownList>

    <br />
    <br />
    <b>Grid View:</b>
    <br />
    <br />

    <asp:GridView ID="GridView1" runat="server" Height="181px" 
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
        Width="518px">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowEditButton="True" />
        </Columns>
        <EmptyDataTemplate>
            &nbsp;
        </EmptyDataTemplate>
    </asp:GridView>
    </form>

</body>
</html>

.aspx页面代码:

namespace DataGridView_Sample
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=SHINY-PC\\SQLEXPRESS;Initial Catalog=NRK;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                con.Open();
                cmd = new SqlCommand("Select name from sys.tables order by name", con);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("--Select--", "--Select--"));
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex != 0)
            {
                cmd = new SqlCommand("select * from " + DropDownList1.SelectedItem.Value, con);
                con.Open();
                da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                con.Close();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            cmd = new SqlCommand("select * from " + DropDownList1.SelectedItem.Value, con);
            con.Open();
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            GridView1.EditIndex = Convert.ToInt16(e.NewEditIndex);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }
    }
}

请任何人都可以帮助。

提前致谢。

鉴于以上所述,我将创建一个包含两个字段的数据库表:tablename和columnname。 将有4行,其表名= emp,并且每一行的columnname = emp表中的列之一。 类似地,将有6行,其中tablename = dept,每行将具有columnname = dept表中的列之一。 然后,在GridView1_RowUpdating事件中,您可以基于在DropDownList中选择的表从数据库中提取列的名称,并使用对该表使用的任何存储过程进行相应的更新。 在GridView1_RowCancelingEdit中,您只需要做

GridView1.EditIndex = -1;

并重新绑定数据(您将需要一个方法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM