2013-03-03 60 views
1

我正在創建一個包含一個Dropdownlist和Gridview的網頁。從下拉列表動態填充表格並在Gridview中顯示

查詢是Dropdownlist將包含SQL Server數據庫表列表。當我從下拉列表中選擇一個表名時,Gridview需要顯示整個表數據並能夠執行編輯,更新,刪除,取消操作。

當我點擊編輯Gridview需要顯示更新和取消按鈕,它更新應更新dropdownlist表,並刪除。

我的代碼如下這樣:

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) 
     { 
     } 
    } 
} 

請任何一個可以提供幫助。

在此先感謝。

+1

你有什麼問題或錯誤? – 2013-03-03 18:38:28

+0

如果你問的是你如何預測需要更新的列(我假設並非所有的表都具有相同的列名),那麼您需要即時構建更新語句。也許你可以將列信息存儲在一個包含2列,表名和列名的表中?然後從這些信息構建更新聲明? – Melanie 2013-03-05 22:09:46

+0

代碼爲GridView1_RowUpdating,GridView1_RowCancelingEdit事件。它使用Dropdownlist動態地將數據從sql表填充到Gridview。 – user2128849 2013-03-06 09:51:49

回答

0

鑑於上述情況,我會創建一個包含兩個字段的數據庫表:tablename和columnname。將有4行tablename = emp,每行將有一個columnname = emp表中的一列。同樣,將有6行where tablename = dept,每行將有一個columnname = dept表中的一列。然後,在GridView1_RowUpdating事件中,可以根據DropDownList中選擇的表從數據庫中提取列的名稱,並使用該表的任何存儲過程進行相應更新。在GridView1_RowCancelingEdit,您需要做的僅僅

GridView1.EditIndex = -1; 

,並重新綁定您的數據(你所需要的是一種方法),你就大功告成了。